object c Dictionaries

你需把一组对象以 K-Value(键值对)的形式保存起来,数组没办法做到。 

NSNumber *age = [NSNumber numberWithUnsignedInteger:51];

NSDictionary *person = [[NSDictionaryalloc] initWithObjectsAndKeys:

@"Anthony", 

@"First Name",

@"Robbins", 

@"Last Name",

age, 

@"Age",
nil];


NSNumber *age = [NSNumber numberWithUnsignedInteger:51];NSMutableDictionary *person = [[NSMutableDictionaryalloc]initWithObjectsAndKeys:
@"Anthony", @"First Name",

@"Robbins", @"Last Name",age, @"Age",

nil];
[personremoveObjectForKey:@"Age"];
NSLog(@"First Name = %@", [person objectForKey:@"First Name"]);NSLog(@"Last Name = %@", [person objectForKey:@"Last Name"]);NSLog(@"Age = %@", [person objectForKey:@"Age"]);



NSNumber *age = [NSNumber numberWithUnsignedInteger:51];NSDictionary *person = [[NSDictionaryalloc] initWithObjectsAndKeys:@"Anthony", @"First Name",
@"Robbins", @"Last Name",

age, @"Age",
nil];
[personenumerateKeysAndObjectsUsingBlock:^(__strong id key, __strong id obj, BOOL *stop) {NSLog(@"Key = %@, Object For Key = %@", key, obj);

}];



for (id keyInDictionary in [person allKeys]){
idobjectForKey = [person objectForKey:keyInDictionary];
NSLog(@"Key = %@, Object For Key = %@", keyInDictionary, objectForKey);} 


SEnumerator *keys = [person keyEnumerator];
idkeyInDictionary = nil;
while ((keyInDictionary = [keys nextObject]) != nil){
idobjectForKey = [person objectForKey:keyInDictionary];
NSLog(@"Key = %@, Object For Key = %@", keyInDictionary, objectForKey);} 


你可能感兴趣的:(object c Dictionaries)