OC NSDictionary 的常用方法
//老王小贴士快捷方式提高效率神器 @ + esc //可变的不建议使用 NSArray *array = @[@"1",@"2"]; NSDictionary *d =@{@"a": @"A",@"b": @"B"}; //什么是字典//NSDictionary //保存键值对(key - value)的容器
//老王小贴士快捷方式提高效率神器 @ + esc
//可变的不建议使用
NSArray *array = @[@"1",@"2"];
NSDictionary *d = @{@"a": @"A", @"b": @"B"};
//什么是字典//NSDictionary
//保存键值对(key - value) 的容器
//一个key 对应一个value(实体)
//创建字典对象
//类方法
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"沐风", @"name", @"呵呵", @"sex", @"金紫荆韩式松骨", @"address", @"呵呵", @"exo", nil];
//对象方法
NSDictionary *dictionary1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"沐风", @"name", @"呵呵", @"sex", @"金紫荆韩式松骨", @"address", nil];
//创建空字典
NSDictionary *dictionaty2 = [NSDictionary dictionary];
NSLog(@"%@", dictionary);
//获取所有key值
NSLog(@"%@", dictionary.allKeys);//allkeys 属性
//获取所有value值
NSLog(@"%@", dictionary.allValues);
//根据key查value
NSLog(@"%@", [dictionary objectForKey:@"name"]);
//根据value查询key
NSLog(@"%@", [dictionary allKeysForObject:@"呵呵"]);
//字典中 一个key 只能对应一个value 但是一个value 可以对应多个key
//间接遍历字典(根据allkeys数组获取字典中所有的key - value对)
for (NSString *key in dictionary.allKeys ) {
NSLog(@"%@", [dictionary objectForKey:key]);
}
//NSMutableDictionary
//初始化空的可变字典、
NSArray *array = [NSArray array];
NSMutableDictionary *mubDic = [NSMutableDictionary dictionary];
//添加新的键值对
[mubDic setObject:@"沐风" forKey:@"70分"];
[mubDic setObject:@"大汗" forKey:@"71分"];
NSLog(@"%@", mubDic);
//修改key对应的value
//本质是重新赋值
[mubDic setObject:@"大汗" forKey:@"70分"];
[mubDic setObject:@"沐风" forKey:@"71分"];
//删除键值
[mubDic removeObjectForKey:@"70分"];
NSLog(@"%@", mubDic);
//删除多个键值
[mubDic removeObjectsForKeys:(NSarray *)];
//删除全部键值
[mubdic removeAllObjects];
更多推荐
所有评论(0)