NSDirctionary
NSDirctionary(字典),以键值对的形式存放数据,常用方法如下:
创建
NSObject *obj1 = [NSObject new];
NSObject *obj2 = [NSObject new];
//对象方法,对应的类方法以dictionaryWith开头
NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:obj1,@"obj1",obj2,@"obj2", nil];
NSDictionary *dict2 = [[NSDictionary alloc]initWithObjects:@[obj1,obj2] forKeys:@[@"obj1",@"obj2"]];
NSDictionary *dict3 = @{@"obj1":obj1,@"obj2":obj2};
//单个元素的字典
NSDictionary *dict4 = [NSDictionary dictionaryWithObject:obj1 forKey:@"obj1"];
获取所有的key
NSArray *allKeys = [dict allKeys];
获取所有的value
NSArray *allValues = [dict allValues];
通过key获取value
NSObject *value = [dict1 objectForKey:@"obj1"];
NSObject *value1 = dict1[@"obj1"];
NSMutableDirctionary
NSMutableDirctionary(可变字典),继承于NSDictionary,并扩充了增删改功能:
创建
NSObject *obj1 = [NSObject new];
NSObject *obj2 = [NSObject new];
NSMutableDictionary *md1 = [[NSMutableDictionary alloc] initWithCapacity:3];
NSMutableDictionary *md2 = [[NSMutableDictionary alloc]initWithObjects:@[obj1,obj2] forKeys:@[@"obj1",obj2]];
NSMutableDictionary *md3 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:obj1,@"obj1",obj2,@"obj2", nil];
NSMutableDictionary *md4 = [[NSMutableDictionary alloc]initWithDictionary:dic1];






网友评论