I started Objective-C programming about a week ago and am stuck on creating a new string from an array of strings passed into the function. I’ve researched this thoroughly and can’t seem to figure out why I can use stringByAppendingString once (checked with NSLog) but not a second time without throwing this error:
Thanks for looking.
2012-06-29 20:46:35.761 Calculator[32883:f803] -[__NSCFNumber stringByAppendingString:]: unrecognized selector sent to instance 0x6e43d50
2012-06-29 20:46:35.763 Calculator[32883:f803] * Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[__NSCFNumber stringByAppendingString:]: unrecognized selector sent to instance 0x6e43d50’
+ (NSString *)descriptionOfTopOfStack:(NSMutableArray *)stack
{
NSString *result;
NSString *displayString = [[NSString alloc]init];
id topOfStack = [stack lastObject];
if (topOfStack) [stack removeLastObject];
if ([self isNoOperandOperation:topOfStack]) {
displayString = [displayString stringByAppendingString:topOfStack];
result = displayString;
}
else if ([self isOperation:topOfStack]){
id nextInStack = [stack lastObject];
if (topOfStack) [stack removeLastObject];
displayString = [displayString stringByAppendingString:nextInStack];
displayString = [displayString stringByAppendingString:topOfStack];
result = displayString;
stringByAppendingString is a method of NSString, you have to return your displayString as NSString,
EDIT: stringByAppendingString method requires parameter type of NSString, your topOfStack is type ID, you need to type cast this to NSString assuming your topOfStack is string, same for nextInStack