-1

I have a "bookshelf" array that contains four NSMutableDictionary "book" (which contains three properties), my question is how do I access the hasBeenRead flag/property of the "book" dictionary to find out which book has not been read?

Book(s):

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

Bookshelf:

[
    {
         "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"
    }
]

I can get the Book NSMutableDictionary:

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

Thanks.

0--
  • 221
  • 1
  • 6
  • 16
  • Is `readBook` really a string? Try logging its `class`. – Wain Jul 15 '14 at 19:29
  • In other words, "How do I use NSArray and NSDictionary?" Two objects that anyone programming in Objective-C should be intimately familiar with. – Hot Licks Jul 16 '14 at 01:00

2 Answers2

0

How about:

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

}

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35
0

you can filter your bookshelf use NSPredicate

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

now you can get all book has not been read

Regards

Read more

Huy Nghia
  • 996
  • 8
  • 22