我在大约一周前开始使用Objective-C编程,并且一直在从传递给函数的字符串数组创建一个新字符串。我对此进行了彻底的研究,似乎无法弄清楚为什么我可以使用stringByAppendingString
一次(使用NSLog
检查)但不是第二次没有抛出此错误:
感谢您的光临。
2012-06-29 20:46:35.761 Calculator [32883:f803] - [__ NSCFNumber stringByAppendingString:]:无法识别的选择器发送到实例0x6e43d50
2012-06-29 20:46:35.763计算器[32883:f803] * 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSCFNumber stringByAppendingString:]:发送无法识别的选择器例如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;
答案 0 :(得分:1)
stringByAppendingString是NSString的一个方法,你必须将displayString作为NSString返回,
+ (NSString *)descriptionOfTopOfStack:(NSMutableArray *)stack
{
NSLog(@"stack array :: %@",stack);
NSString *result;
NSString *displayString = [[NSString alloc]init];
id topOfStack = [stack lastObject];
if (topOfStack) [stack removeLastObject];
NSLog(@"topOfStack :: %@", (NSString*)topOfStack);
if ([self isNoOperandOperation:topOfStack]) {
displayString = [[(NSNumber*)displayString stringValue]
stringByAppendingString:(NSString*)topStack];
result = displayString;
}
else if ([self isOperation:topOfStack]){
id nextInStack = [stack lastObject];
if (topOfStack) [stack removeLastObject];
displayString = [[(NSNumber*)displayString stringValue]
stringByAppendingString:(NSString*)nextInStack];
displayString = [[(NSNumber*) displayString stringValue]
stringByAppendingString:(NSString*)topStack];
result = displayString;
编辑:stringByAppendingString方法需要NSString的参数类型,你的topOfStack是类型ID,你需要输入强制转换为NSString,假设你的topOfStack是字符串,对于nextInStack是相同的
答案 1 :(得分:0)
您需要先将id
转换为字符串。对于NSNumbers,您可以使用-stringValue
。