http://blog.csdn.net/ztp800201/article/details/7777748
NSDictionary的作用同JAVA中的字典相同,提供了“健-值”对的集合。
比如,使用字典实现员工编号到员工姓名的存放,编号是一个键(唯一性),姓名是值.
NSDictionary例子:
-
-
NSDictionary *employes = [NSDictionary dictionaryWithObjectsAndKeys:@"张三",@"1", @"李四",@"2",@"王五",@"3",nil];
-
NSString *firstEmployes = [employes objectForKey:@"1"];
NSMutableDictionary例子:
-
-
NSMutableDictionary *employes = [NSMutableDictionary dictionary];
-
[employes setObject:@"张三" forKey:@"1"];
-
[employes setObject:@"李四" forKey:@"2"];
-
[employes setObject:@"王五" forKey:@"3"];
-
-
-
NSLog(@"No:1,%@",[employes objectForKey:@"1"]);
-
NSLog(@"No:2,%@",[employes objectForKey:@"2"]);
-
NSLog(@"No:3,%@",[employes objectForKey:@"3"]);
【程序输出】
NSDictionary常用方法:
-
+(id)dictionaryWithObjectsAndKeys:obj1,obj2,...nil
-
-(id)initWithObjectsAndKeys:obj1,key1,obj2,key2,...nil
-
-(unsigned int)count
-
-(NSEnumerator*)keyEnumerator
-
-(NSArray*)keysSortedByValueUsingSelector:(SEL)selector
-
-(NSEnumerator*)objectEnumerator
-
-(id)objectForKey:key
NSMutableDictionary常用方法:
-
+(id)dictionaryWithCapacity:size
-
-(id)initWithCapacity:size
-
-(void)removeAllObjects
-
-(void)removeObjectForKey:key
-
-(void)setObject:obj forKey:key
-
阅读(367) | 评论(0) | 转发(0) |