Thursday, May 9, 2013

NSDictionary objectsForKeys example ios


objectsForKeys :notFoundMarker:

Returns the set of objects from the dictionary that corresponds to the specified keys as an NSArray.
- (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(id)anObject
Parameters
keys
An NSArray containing the keys for which to return corresponding values.
anObject
The marker object to place in the corresponding element of the returned array if an object isn’t found in the dictionary to correspond to a given key.
Discussion of [NSDictionary objectsForKeys]
The objects in the returned array and the keys array have a one-for-one correspondence, so that the nthe object in the returned array corresponds to the nthe key in keys.
Example of [NSDictionary objectsForKeys]
NSArray *section0Keys = [NSArray arrayWithObjects:@"city", @"phone", @"email", nil];
NSMutableArray *section0 = [[[feedDictionary objectsForKeys:section0Keys notFoundMarker:[NSNull null]] mutableCopy] autorelease];
if ([section0 indexOfObject:[NSNull null]] != NSNotFound) {
    isValid = NO;
}
// you don't need this if you don't care for invalid arrays.
[section0 removeObjectsInArray:[NSArray arrayWithObject:[NSNull null]]];

NSArray *section1Keys = [NSArray arrayWithObjects:@"year", @"degree", nil];
NSMutableArray *section1 = [[[feedDictionary objectsForKeys:section1Keys notFoundMarker:[NSNull null]] mutableCopy] autorelease];
if ([section1 indexOfObject:[NSNull null]] != NSNotFound) {
    isValid = NO;
}
[section1 removeObjectsInArray:[NSArray arrayWithObject:[NSNull null]]];

self.dataArray = [NSArray arrayWithObjects:
section0,
section1,
nil];
Example of [NSDictionary objectsForKeys]
[[dictionary objectsForKeys:[NSArray arrayWithObject:@"myKey"]
             notFoundMarker:@"defaultValue"]
 objectAtIndex:0]
Example of [NSDictionary objectsForKeys]
NSMutableArray *objects = [myDictionary objectsForKeys:myKeys notfoundMarker:[NSNull null]];
  for (int ii = 0; ii < [objects count]; ii++ ) {
      if ([objects objectAtIndex:ii] == [NSNull null]) {
          [objects insertObject:[myKeys objectAtIndex:ii] atIndex:ii];
      }
  }