swift基础-5-字典

字典定义: 键值对
OC:

NSDictionary *dict = [NSDictionary dictionaryWithObject:@"solozyx" forKey:@"name"];
NSLog(@"%@", dict);

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"name", @"solozyx", @"age", @30, nil];
NSLog(@"%@", dict);

NSDictionary *dict = @{@"name":@"solozyx", @"age":@99};
NSLog(@"%@", dict);

key一定要是可以hash的(String, Int, Float, Double, Bool), value没有要求
swift:

var dict = ["name":"solozyx", "age":99]
print(dict)

var dict1:Dictionary = ["name":"solozyx", "age":99]
print(dict1)

var dict2:Dictionary = ["name":"solozyx", "age":99]
print(dict2)

var dict3:[String:Any] = ["name":"solozyx", "age":99]
print(dict3)

var dict4:[String:Any] = Dictionary(dictionaryLiteral: ("name", "solozyx"), ("age", 99))
print(dict4)

可变字典:

var dict5 = [:]

不可变字典:

let dict6  = [:]

字典操作
OC:

//1.获取
NSDictionary *dict = @{@"name":@"solozyx", @"age":@99};
NSLog(@"%@", dict[@"name"]);

//2.修改
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"solozyx", @"age", @99, nil];
dict[@"name"] = @"iversion";
NSLog(@"%@", dict[@"name"]);

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"solozyx", @"age", @99, nil];
[dict setObject:@"iversion" forKey:@"name"];
NSLog(@"%@", dict[@"name"]);

swift:

//1.获取
var dict7 = ["name":"solozyx", "age":99]
print(dict7["name"]!)

//2.修改
var dict8 = ["name":"solozyx", "age":99]
dict8["name"] = "iverson"
print(dict8["name"]!)

var dict9 = ["name":"solozyx", "age":99]
dict9.updateValue("iverson", forKey: "name")
print(dict9["name"]!)

// 3.更新
// updateValue返回一个可选类型, 如果字典中不存在需要更新的key, 那么返回nil, 如果存在返回原始值
var dict10 = ["name":"solozyx", "age":99]
if let orignal = dict10.updateValue("iverson", forKey: "name"){
    print(dict10["name"]!)
    print(orignal)
}

// updateValue返回一个可选类型, 如果字典中不存在需要更新的key, 那么返回nil并且会将新的键值对添加到字典中
var dict11 = ["name":"solozyx", "age":99]
if let orignal = dict11.updateValue("iverson", forKey: "abc"){
    print(dict11["abc"]!)
    print(orignal)
}
print(dict11)

4.添加
OC:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"solozyx", @"age", @99, nil];
dict[@"height"] = @172;
NSLog(@"%@", dict);

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"solozyx", @"age", @99, nil];
[dict setObject:@172 forKey:@"height"];
NSLog(@"%@", dict);

swift:

var dict12 = ["name":"solozyx", "age":99]
dict12["height"] = 172;
print(dict12)

5.删除
OC:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"solozyx", @"name", @99, @"age", nil];
[dict removeObjectForKey:@"name"];
NSLog(@"%@", dict);

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"solozyx", @"name", @99, @"age", nil];
[dict removeAllObjects];
NSLog(@"%@", dict);

swift:

var dict13 = ["name":"solozyx", "age":99]
dict13.removeValueForKey("name")
print(dict13)
// removeValueForKey返回一个可选类型, 如果字典中不存在需要删除的key, 那么返回nil并且不会执行任何操作, 如果存在则删除key对应的值, 并且返回被删除的值
var dict14 = ["name":"solozyx", "age":99]
if let orignal = dict14.removeValueForKey("names") {
    print(dict14)
    print(orignal)
}
print(dict14)

var dict15 = ["name":"solozyx", "age":99]
dict15.removeAll(keepCapacity: true)

遍历字典
OC:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"solozyx", @"name", @99, @"age", nil];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"key = %@ value = %@", key, obj);
}];

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"solozyx", @"name", @99, @"age", nil];
NSArray *keys = [dict allKeys];
for (NSString *key in keys) {
NSLog(@"%@", key);
}

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"solozyx", @"name", @99, @"age", nil];
NSArray *values = [dict allValues];
for (NSString *value in values) {
NSLog(@"%@", value);
}

swift:

var dict16 = ["name":"solozyx", "age":99]
for (key , value) in dict16 {
    print("key = \(key) value = \(value)")
}

var dict17 = ["name":"solozyx", "age":99]
for key in dict17.keys {
    print("key = \(key)")
}

var dict18 = ["name":"solozyx", "age":99]
for value in dict18.values{
    print("value = \(value)")
}

你可能感兴趣的:(swift基础-5-字典)