应用程序在enumerateObjectsUsingBlock中崩溃

时间:2014-01-21 09:20:51

标签: ios objective-c-blocks

我的申请因以下代码而崩溃:

  

NSMutableArray* array = [dict objectForKey:key];
  if (array)
  {
  __block BOOL find = NO;
  [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  NSString* word = (NSString*)obj;
  if ([word isEqualToString:@"jerry"]) {
  *stop = YES;
  find = YES;
  }
  }];

崩溃信息是:

  

1 CoreFoundation 0x2ecd30f2 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke (in CoreFoundation) + 90
  2 CoreFoundation 0x2ecd3024 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] (in CoreFoundation) + 232

我无法弄清楚,有谁知道如何解决这个问题?

4 个答案:

答案 0 :(得分:2)

尝试使用:

[word isEqualToString:@"jerry"]

答案 1 :(得分:1)

试试这个:

[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
     NSString* word = (NSString*)obj;
     if ([word isEqualToString:@"jerry"]) {
          stop = YES;
          find = YES;
     }

您正在以错误的方式比较字符串。

答案 2 :(得分:0)

试试这个

NSMutableArray* array = [NSMutableArray arrayWithArray:[dict objectForKey:key]];
if (array)
{
     __block BOOL find = NO;
     [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
     NSString* word = (NSString*)obj;
     if ([word isEqualToString:@"jerry"]) {
          *stop = YES;
          find = YES;
     }
}];

答案 3 :(得分:0)

我的钱在你的阵列上变成了实际上不是阵列。当您从dict中的对象创建和设置数组时,请检查[dict objectForKey:key]实际上是否为数组。

NSMutableArray* array = [dict objectForKey:key];
NSLog(@"class of array:  %@", [[dict objectForKey:key] class]);