Tuesday, May 7, 2013

NSDictionary dictionaryWithObjectsAndKeys example ios


dictionaryWithObjectsAndKeys:

Creates and returns a dictionary containing entries constructed from the specified set of values and keys.
+ (id)dictionaryWithObjectsAndKeys:(id)firstObject , ...
Parameters
firstObject
The first value to add to the new dictionary.
...
First the key for firstObject, then a null-terminated list of alternating values and keys. If any key is nil, an NSInvalidArgumentException is raised.
Discussion of [NSDictionary dictionaryWithObjectsAndKeys]
This method is similar to dictionaryWithObjects:forKeys:, differing only in the way key-value pairs are specified.
Example of [NSDictionary dictionaryWithObjectsAndKeys]
(NSMutableDictionary *)dictionary {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          self.userID, @"id",
                          self.userName, @"name",
                          self.userSurname, @"sName",
                          self.userNickname, @"nName",
                          self.userPassword, @"pwd",
                          [NSNumber numberWithDouble:[self.userBirthday timeIntervalSince1970] * 1000], @"birthday",
                          self.userSex, @"sex",
                          self.userEmail, @"email",
                          self.userLanguage, @"locale",
                          [NSNumber numberWithLongLong:self.userCoin], @"coin",
                          self.userRate, @"rate",
                          [NSNumber numberWithDouble:self.userCoordinate.latitude], @"lat",
                          [NSNumber numberWithDouble:self.userCoordinate.longitude], @"lon",
                          self.userPlaces, @"userPlaces",
                          nil];
}
Example of [NSDictionary dictionaryWithObjectsAndKeys]
- (NSMutableArray *)getCategoriesForChannel:(int)channelId {
    NSDictionary *data = [self call:@"get_categories.ashx"];
    NSArray *categories = [data objectForKey:@"categories"];
    NSMutableArray *returnArray = [NSMutableArray
                          arrayWithCapacity:[categories count]];

    for(NSDictionary *category in categories) {
        [returnArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                         [category objectForKey:@"Channel_id"], @"id",
                         [category objectForKey:@"Channel_name"], @"name", nil]];
    }
    return returnArray;
}