如何知道我的数组中是否包含字典?

3

我有一个这样的数组

   ( {
    GPSOdometerReading = "11843.6";
    "_id" =         {
        "$oid" = 5656e7175201edbb16a483f4;
    };
    acc = 0;
    )

那么如何知道我的数组是否包含该字典呢?谢谢。
2个回答

10
你需要枚举数组并像下面这样检查每个对象的类:
BOOL isContainsDict = NO;
for (id value in Array){
    if ([value isKindOfClass:[NSDictionary class]]){
        isContainsDict = YES;
        break; // end since you only want to know if the array contains an instance of dictionary. if it does no need to continue the loop just break and perform the process
      }
}

好的答案,非常简短。 - user3182143
isContainsDict 应该被初始化。 - Droppy

1
你可以使用这段代码:

-(BOOL)arrayContainDictionary
{
     for(int i=0; i<[array count];i++){
         if([array[i] isKindOfClass:[NSDictionary Dictionary]]){
             return YES;
         }
     }
     return NO;
}

1
如果第一个对象不是NSDictionary,则应立即返回NO,无需进一步检查。在for循环之外返回NO。 - Bhumit Mehta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接