【问题标题】:application crashes when appendString for NSMutableString is used使用 NSMutableString 的 appendString 时应用程序崩溃
【发布时间】:2012-07-17 06:06:44
【问题描述】:

我是 Objective-c 的新手,我决定从斯坦福的 CS193s 2010F Session 的讲座/作业开始。

我正在处理第二个作业,当我不得不返回一个组合(连接)NSMutableArray 中的每个字符串的 NSString 时,我陷入了困境。 (MutableArray 在其每个索引处仅包含 NSString)

我的方法是使用 for 循环来传递 MutableArray 的索引(在下面的代码中,MutableArray 是 'anExpression' 类型为 'id')。我声明了一个 NSMutableString 并在“anExpression”数组的每个索引处添加了 NSString。代码如下:

+ (NSString *)descriptionOfExpression:(id)anExpression{
  NSMutableString *result = [NSMutableString string];

  for (int i = 0;i<[anExpression count];i++){
    [result appendString:[anExpression objectAtIndex:i]];
  }

  return result;
}

然而,在

 [result appendString:[anExpression objectAtIndex:i]];

xcode 崩溃并出现以下错误语句:

2012-07-17 01:44:51.014 Calculator[9470:f803] -[__NSCFNumber length]: unrecognized selector sent to instance 0x68732c0
2012-07-17 01:44:51.015 Calculator[9470:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x68732c0'
*** First throw call stack:
(0x13ca022 0x155bcd6 0x13cbcbd 0x1330ed0 0x1330cb2 0x12d2d18 0x13460d7 0x1397a8d 0x3a50 0x27ed 0x13cbe99 0x1714e 0x170e6 0xbdade 0xbdfa7 0xbd266 0x3c3c0 0x3c5e6 0x22dc4 0x16634 0x12b4ef5 0x139e195 0x1302ff2 0x13018da 0x1300d84 0x1300c9b 0x12b37d8 0x12b388a 0x14626 0x1ed2 0x1e45 0x1)
terminate called throwing an exception

我查看了苹果的开发者文档,看到了 'NSString stringWithFormat:' 方法,决定改用这个方法:

+ (NSString *)descriptionOfExpression:(id)anExpression{
  NSMutableString *result = [NSMutableString string];

  for (int i = 0;i<[anExpression count];i++){
    [result appendString:[NSString stringWithFormat:@"%@",[anExpression objectAtIndex:i]]];
  }

  return result;
}

现在可以使用了。

现在我很困惑为什么第二个代码有效但第一个无效。 我认为附加字符串只有在传递 nil 时才会失败(并崩溃)......

我有什么遗漏吗?

提前谢谢你:)

【问题讨论】:

    标签: iphone xcode nsmutablearray nsmutablestring


    【解决方案1】:

    看起来 anExpression 的内容是 NSNumber 而不是 NSString 的实例。您得到的错误是关于 appendString 如何工作的提示;第一步显然是询问传递的“字符串”有多长(大概是为了分配足够的内存)。这显然不是 NSNumber 上的方法(因此崩溃),stringWithFormat 旨在进行类型检查并更加灵活。

    我怀疑你也可以使用stringValue

    [result appendString:[[anExpression objectAtIndex:i] stringValue]];
    

    【讨论】:

    • 啊...非常感谢,ctrahey。我太粗心了!看来我的数组也确实有 NSNumber,而不仅仅是 NSStrings。感谢 ctrahey 和 Raj。我真的很感激:)
    【解决方案2】:
    + (NSString *)descriptionOfExpression:(id)anExpression{
    
          NSMutableString *result = [NSMutableString string];
    
              for (int i = 0;i<[anExpression count];i++) {
    
                  [result appendString:[[anExpression objectAtIndex:i] stringValue]];
    
             }
    
      return result;
    }
    

    将其转换为字符串会对您有所帮助!

    【讨论】:

      【解决方案3】:

      使用此功能:

      + (NSString *)descriptionOfExpression:(id)anExpression
      {
           NSMutableString *result = [[NSMutableString alloc] init];
      
          for (int i = 0;i<[anExpression count];i++)
          {
              NSString *str = [NSString stringWithFormat:@"%@",[anExpression objectAtIndex:i]]; 
      
              if(str)
              { 
                  [result appendString:str];
              }
      
              //OR
              //[result appendFormat:[NSString stringWithFormat:@"%@",[anExpression objectAtIndex:i]]]; 
      
          }
      
          return result;
      }
      

      【讨论】:

      • 感谢您的回答。我注意到您使用 if(str) 检查数组中的字符串为 nil 的情况。但是,我遇到了麻烦,为什么第一个代码会崩溃,而第二个代码(这是您提供的类似代码)可以正常工作。
      • 是的,我现在明白问题所在了!真是愚蠢的错误。感谢您的帮助!
      猜你喜欢
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 2017-07-29
      • 2018-06-08
      • 2014-04-18
      • 2023-03-04
      • 2015-04-24
      • 1970-01-01
      相关资源
      最近更新 更多