2011年10月29日 星期六

Objective C 如何使用Dictionary

//create the dictionary
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];


//add keyed data
[dictionary setObject:@"Orange" forKey:@"Peggy"];
[dictionary setObject:@"Purple" forKey:@"Landy"];
[dictionary setObject:@"Red" forKey:@"Ula"];


//write out "Orange" to the log
NSLog(@"%@", [dictionary objectForKey:@"Peggy"]);


// values in foreach loop -1
NSLog(@"%@", dictionary );
NSArray *keys = [dictionary allKeys];
for (NSString *key in keys) {
NSLog(@"%@ is %@",key, [dictionary objectForKey:key]);
}

//values in foreach loop -2
for (id theKey in dictionary ) {
id anObject = [[aDictionary objectForKey:theKey] retain];
}

//release the dictionary
[dictionary release];

沒有留言:

張貼留言