如何使用快速枚举访问数组中的NSMutableDictionary属性?

时间:2014-07-15 19:22:07

标签: ios for-loop nsmutablearray nsmutabledictionary fast-enumeration

我有一个“bookshelf”数组,其中包含四个NSMutableDictionary“book”(其中包含三个属性),我的问题是如何访问“book”字典的hasBeenRead标志/属性以找出哪本书没有读?

书(S):

 {
     "title":"Winnie-the-Pooh",
     "author":"A.A. Milne"
     "hasBeenRead": No
 }

书架:

[
    {
         "title":"Jungle Book",
         "author":"Rudyard Kipling",
         "hasBeenRead":"Yes"
    },
    {
         "title":"Winnie-the-Pooh",
         "author":"A.A. Milne",
         "hasBeenRead":"No"
    },
    {
         "title":"Alice In Wonderland",
         "author":"Lewis Carroll",
         "hasBeenRead":"Yes"
    }
]

我可以获得Book NSMutableDictionary:

for (NSString* readBook in self.bookshelf) {
    NSLog(@"Books: %@", readBook);
}

感谢。

2 个答案:

答案 0 :(得分:0)

怎么样:

for (NSDictionary* readBook in self.bookshelf) {
  NSLog(@"Books: %@", [readBook objectForKey:@"hasBeenRead"]);

}

答案 1 :(得分:0)

您可以过滤书架使用NSPredicate

NSString *hasBeenReadFlag = @"No";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.hasBeeRead == %@", hasBeenReadFlag];
NSArray *filtered = [self.bookshelf filteredArrayUsingPredicate:predicate];
NSLog(@"%@", filtered);

现在你可以得到所有书还没有被阅读

此致

Read more