天天看點

swift基礎文法(10-字典)

定義字典 OC: NSDictionary *dict =[NSDictionary dictionaryWithObject:                                   @“qbs" forKey:@"name"]; NSLog(@"%@", dict); 輸出結果: 2016-01-06 15:09:11.214 OCTest[3773:761032] {

    name = gxq; } NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:                                   @"name", @" qbs",                                   @"age", @20, nil]; NSLog(@"%@", dict); 輸出結果: 2016-01-06 15:13:39.853 OCTest[3831:792730] {

    gxq = name;

    20 = age; } NSDictionary *dict = @{@"name":@" qbs", @"age":@20}; NSLog(@"%@", dict); 輸出結果: 2016-01-06 15:14:57.616 OCTest[3841:801710] {

    age = 20;

    name = gxq; } swift: key一定要是可以hash的(String, Int, Float, Double, Bool) value沒有要求 var dict = ["name":" qbs", "age":20] print(dict)

var dict1:Dictionary = ["name":" qbs", "age":20] print(dict1)

var dict2:Dictionary<String,AnyObject> = ["name":" qbs", "age":20] print(dict2)

var dict3:[String:AnyObject] = ["name":" qbs", "age":20] print(dict3)

var dict4:[String:AnyObject] = Dictionary(dictionaryLiteral: ("name", " qbs"), ("age", 20)) print(dict4) 輸出結果: ["age": 20, "name": qbs] ["age": 20, "name": qbs ] ["age": 20, "name": qbs ] ["age": 20, "name": qbs ] ["age": 20, "name": qbs ]   可變字典: var dict5 = [:] 不可變字典: let dict6  = [:]   字典操作 OC: 擷取 NSDictionary *dict = @{@"name":@" qbs", @"age":@20}; NSLog(@"%@", dict[@"name"]); 輸出結果: 2016-01-06 15:26:00.351 OCTest[3923:881138] gxq   修改 NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:                      @"name", @" qbs",                      @"age", @20, nil]; dict[@"name"] = @"iversion"; NSLog(@"%@", dict[@"name"]);   NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:                     @"name", @" qbs",                     @"age", @20, nil]; [dict setObject:@"iversion" forKey:@"name"]; NSLog(@"%@", dict[@"name"]);   輸出結果: 2016-04-01 15:27:01.704 OCTest[3933:890317] iversion 2016-04-01 15:28:21.398 OCTest[3943:899613] iversion   swift: 擷取 var dict = ["name":" qbs", "age":20] print(dict["name"]!) 輸出結果: gxq     修改 var dict = ["name":" qbs", "age":20] dict["name"] = "iverson"

print(dict["name"]!)

var dict1 = ["name":" qbs", "age":20] dict1.updateValue("iverson", forKey: "name") print(dict["name"]!) 輸出結果: iverson iverson   更新 updateValue傳回一個可選類型 如果字典中不存在需要更新的key, 那麼傳回nil, 如果存在傳回原始值 var dict = ["name":" qbs", "age":25] if let orignal = dict.updateValue("iverson", forKey: "name")

{

    print(dict["name"]!)

    print(orignal) } 輸出結果: iverson qbs   updateValue傳回一個可選類型 如果字典中不存在需要更新的key, 那麼傳回nil并且會将新的鍵值對添加到字典中 var dict = ["name":" qbs", "age":25] if let orignal = dict.updateValue("iverson", forKey: "abc")

{

    print(dict["abc"]!)

    print(orignal)

} print(dict) 輸出結果: ["abc": iverson, "age": 25, "name": qbs] 添加 OC: NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:                    @"name", @" qbs",                    @"age", @25, nil]; dict[@"height"] = @100; NSLog(@"%@", dict); 輸出結果: 2016-04-01 15:35:11.734 OCTest[4025:946250] {     qbs = name;     25 = age;

    height = 100; }   NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:                     @"name", @“QBS",                      @"age", @30, nil]; [dict setObject:@200 forKey:@"height"]; NSLog(@"%@", dict); 輸出結果: 2016-04-01 15:36:15.768 OCTest[4035:953931] {     QBS = name;     30 = age;

    height = 200; }   swift: var dict = ["name ” :"Qbs", "age":50 ] dict["height"] = 160; print(dict) 輸出結果: ["height": 160, "age": 50, "name": Qbs]     删除 OC: NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:                      @" qbs ",@"name",                      @30,@"age",nil ]; [dict removeObjectForKey:@"name"]; NSLog(@"%@", dict); 輸出結果: 2016-04-01 15:40:37.801 OCTest[4058:981747] {     age = 30; }   NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:                      @“qbs",@"name",                      @30,@"age",nil ]; [dict removeAllObjects]; NSLog(@"%@", dict); 輸出結果: 2016-04-01 15:41:20.705 OCTest[4068:989096] { } swift:   var dict = ["name":" qbs", "age":10] dict.removeValueForKey("name") print(dict) 輸出結果: ["age": 10]   removeValueForKey傳回一個可選類型 如果字典中不存在需要删除的key, 那麼傳回nil并且不會執行任何操作 如果存在則删除key對應的值, 并且傳回被删除的值 var dict = ["name ” : "abs", "age ” :20 ] if let orignal = dict.removeValueForKey("names")

{

    print(dict)

    print(orignal)

} print(dict) 輸出結果: ["age": 30, "name": qbs]   var dict = ["name ” : ”qbs", "age":30] dict.removeAll(keepCapacity: true)   周遊字典 OC: NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:                      @" qbs ",@"name",                      @40,@"age",nil ];[dictenumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL *stop) {       NSLog(@"key = %@ value = %@", key, obj); }]; 輸出結果: 2016-04-01 15:45:59.810 OCTest[4117:1022823] key = name value = qbs 2016-04-01 15:45:59.811 OCTest[4117:1022823] key = age value = 40   NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:                      @“qbs", @"name",                      @40, @"age", nil]; NSArray *keys = [dict allKeys]; for (NSString *key in keys) {     NSLog(@"%@", key); } 輸出結果: 2016-04-01 15:48:07.861 OCTest[4137:1039198] name 2016-04-01 15:48:07.862 OCTest[4137:1039198] age NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:                      @" qbs", @"name",                      @40, @"age", nil]; NSArray *keys = [dict allValues]; for (NSString *key in keys) {     NSLog(@"%@", key); } 輸出結果: 2016-04-01 15:49:20.375 OCTest[4148:1049548] gxq 2016-04-01 15:49:20.376 OCTest[4148:1049548] 40     var dict1 = ["name ” :"Qbs", "age":30] for (key , value) in dict1

{

    print("key = \(key) value = \(value)") } 輸出結果: key = age value = 30 key = name value = Qbs     var dict2 = ["name ” :"Qbs", "age":40] for key in dict2.keys

{

    print("key = \(key)") } 輸出結果: key = age key = name   var dict3 = ["name ” :"Qbs", "age":50] for value in dict3.values

{

    print("value = \(value)") } 輸出結果: value = 50 value = Qbs              

轉載于:https://www.cnblogs.com/jordanYang/p/5378239.html