1. 定义字典
//OC:
NSDictionary *dict =[NSDictionary dictionaryWithObject:@"CDH" forKey:@"name"];
NSLog(@"%@", dict);
//输出结果:
//2016-01-06 15:09:11.214 OCTest[3773:761032] {
// name = CDH;
//}
NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:@"name", @"CDH",
@"age", @20, nil];
NSLog(@"%@", dict);
//输出结果:
//2016-01-06 15:13:39.853 OCTest[3831:792730] {
// CDH = name;
// 20 = age;
//}
NSDictionary *dict = @{@"name":@"CDH", @"age":@20};
NSLog(@"%@", dict);
//输出结果:
//2016-01-06 15:14:57.616 OCTest[3841:801710] {
// age = 20;
// name = CDH;
//}
//swift:
//key一定要是可以hash的(String, Int, Float, Double, Bool)
//value没有要求
var dict = ["name":"CDH", "age":20]
print(dict)
var dict1:Dictionary = ["name":"CDH", "age":20]
print(dict1)
var dict2:Dictionary = ["name":"CDH", "age":20]
print(dict2)
var dict3:[String:AnyObject] = ["name":"CDH", "age":20]
print(dict3)
var dict4:[String:AnyObject] =
Dictionary(dictionaryLiteral: ("name", "CDH"), ("age", 20))
print(dict4)
//输出结果:
//["age": 20, "name": CDH]
//["age": 20, "name": CDH]
//["age": 20, "name": CDH]
//["age": 20, "name": CDH]
//["age": 20, "name": CDH]
//可变字典:
var dict5 = [:]
//不可变字典:
let dict6 = [:]
2. 字典操作
//OC获取
NSDictionary *dict = @{@"name":@"CDH", @"age":@20};
NSLog(@"%@", dict[@"name"]);
//输出结果:
//2016-01-06 15:26:00.351 OCTest[3923:881138] CDH
//Swift获取
var dict7 = ["name":"CDH", "age":20]
print(dict7["name"]!)
//输出结果: CDH
//OC修改
NSMutableDictionary *dict =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"name", @"CDH",
@"age", @20, nil];
dict[@"name"] = @"iversion";
NSLog(@"%@", dict[@"name"]);
NSMutableDictionary *dict =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"name", @"CDH",
@"age", @20, nil];
[dict setObject:@"iversion" forKey:@"name"];
NSLog(@"%@", dict[@"name"]);
//输出结果:
//2016-01-06 15:27:01.704 OCTest[3933:890317] iversion
//2016-01-06 15:28:21.398 OCTest[3943:899613] iversion
//swift修改
var dict8 = ["name":"CDH", "age":20]
dict8["name"] = "iverson"
print(dict8["name"]!)
//输出结果:
//iverson
var dict9 = ["name":"CDH", "age":20]
dict9.updateValue("iverson", forKey: "name")
print(dict9["name"]!)
//输出结果:
//iverson
//swift更新
//updateValue返回一个可选类型
//如果字典中不存在需要更新的key, 那么返回nil, 如果存在返回原始值
var dict10 = ["name":"CDH", "age":25]
if let orignal = dict10.updateValue("iverson", forKey: "name"){
print(dict10["name"]!)
print(orignal)
}
//输出结果:
//iverson
//CDH
//updateValue返回一个可选类型
//如果字典中不存在需要更新的key, 那么返回nil并且会将新的键值对添加到字典中
var dict11 = ["name":"CDH", "age":25]
if let orignal = dict11.updateValue("iverson", forKey: "abc")
{
print(dict11["abc"]!)
print(orignal)
}
print(dict)
//输出结果: ["abc": iverson, "age": 25, "name": CDH]
//OC:往字典中添加键值对
NSMutableDictionary *dict =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"name", @"CDH",
@"age", @25, nil];
dict[@"height"] = @100;
NSLog(@"%@", dict);
//输出结果:
//2016-01-06 15:35:11.734 OCTest[4025:946250] {
// CDH = name;
// 25 = age;
// height = 100;
//}
NSMutableDictionary *dict =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"name", @"CDH",
@"age", @30, nil];
[dict setObject:@200 forKey:@"height"];
NSLog(@"%@", dict);
//输出结果:
//2016-01-06 15:36:15.768 OCTest[4035:953931] {
// CDH = name;
// 30 = age;
// height = 200;
//}
//swift:
var dict12 = ["name":"CDH", "age":50]
dict12["height"] = 160;
print(dict12)
//输出结果: ["height": 160, "age": 50, "name": CDH]
//OC:
NSMutableDictionary *dict =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"CDH",@"name",
@30,@"age",nil];
[dict removeObjectForKey:@"name"];
NSLog(@"%@", dict);
//输出结果:
//2016-01-06 15:40:37.801 OCTest[4058:981747] {
// age = 30;
//}
NSMutableDictionary *dict =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"CDH",@"name",
@30,@"age",nil];
[dict removeAllObjects];
NSLog(@"%@", dict);
//输出结果:
//2016-01-06 15:41:20.705 OCTest[4068:989096] {
//}
//swift:
var dict13 = ["name":"CDH", "age":10]
dict13.removeValueForKey("name")
print(dict13)
//输出结果: ["age": 10]
//removeValueForKey返回一个可选类型
//如果字典中不存在需要删除的key, 那么返回nil并且不会执行任何操作
//如果存在则删除key对应的值, 并且返回被删除的值
var dict14 = ["name":"CDH", "age":20]
if let orignal = dict14.removeValueForKey("names"){
print(dict14)
print(orignal)
}
print(dict)
//输出结果:["age": 30, "name": CDH]
var dict15 = ["name":"CDH", "age":30]
dict15.removeAll(keepCapacity: true)
//OC:
NSMutableDictionary *dict =
[NSMutableDictionary dictionaryWithObjectsAndKeys:@"CDH",@"name",
@40,@"age",nil];
[dictenumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL*stop) {
NSLog(@"key = %@ value = %@", key, obj);
}];
//输出结果:
//2016-01-06 15:45:59.810 OCTest[4117:1022823] key = name value = CDH
//2016-01-06 15:45:59.811 OCTest[4117:1022823] key = age value = 40
NSMutableDictionary *dict =
[NSMutableDictionary dictionaryWithObjectsAndKeys:@"CDH", @"name",
@40, @"age", nil];
NSArray *keys = [dict allKeys];
for (NSString *key in keys) {
NSLog(@"%@", key);
}
//输出结果:
//2016-01-06 15:48:07.861 OCTest[4137:1039198] name
//2016-01-06 15:48:07.862 OCTest[4137:1039198] age
NSMutableDictionary *dict =
[NSMutableDictionary dictionaryWithObjectsAndKeys:@"CDH", @"name",
@40, @"age", nil];
NSArray *keys = [dict allValues];
for (NSString *key in keys) {
NSLog(@"%@", key);
}
//输出结果:
//2016-01-06 15:49:20.375 OCTest[4148:1049548] CDH
//2016-01-06 15:49:20.376 OCTest[4148:1049548] 40
// swift
var dict16 = ["name":"CDH", "age":30]
for (key , value) in dict16{
print("key = \(key) value = \(value)")
}
//输出结果:
//key = age value = 30
//key = name value = CDH
var dict17 = ["name":"CDH", "age":40]
for key in dict17.keys{
print("key = \(key)")
}
//输出结果:
//key = age
//key = name
var dict18 = ["name":"CDH", "age":50]
for value in dict18.values{
print("value = \(value)")
}
//输出结果:
//value = 50
//value = CDH