-1

我想从一个NSDictionary

一次 10 个,所以 objectAtIndex 0 到 9

现在只添加:

AddObject:[[[json objectForKey:@"one"] objectAtIndex:1] ObjectForKey:@"likes"];

那9次,有没有更简单的方法可以做到这一点,并且只在对象存在时添加。

提前致谢

4

2 回答 2

1

试试这个:

NSArray *shotsArr = [json objectForKey:@"shots"];
NSMutableArray *allViewsCounts = [NSMutableArray array];
for (NSDictionary *shotDict in shotsArr) {
    NSNumber *viewsCount = [shotDict objectForKey:@"views_count"];
    [allViewsCounts addObject:viewsCount];
}

如果您希望此数组中只有 10 个对象:

__block NSMutableArray *allViewsCounts = [NSMutableArray array];
[shotsArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSDictionary *shotDict = obj;
    NSNumber *viewsCount = [shotDict objectForKey:@"views_count"];
    [allViewsCounts addObject:viewsCount];
    if (idx == 10) {
        *stop = YES;
    }
}];
于 2012-12-29T14:47:16.720 回答
0

简单地使用 for 循环

for (int i=0;i<10;i++){
     if([[[json objectForKey:@"one"] objectAtIndex:i] ObjectForKey:@"likes"]){
          AddObject:[[[json objectForKey:@"one"] objectAtIndex:i] ObjectForKey:@"likes"];
     }
}
于 2012-12-29T12:41:34.580 回答