Swift # 字典

來源:互聯網
上載者:User

標籤:

//: Playground - noun: a place where people can playimport UIKitvar str = "Hello, playground"/*字典: 儲存一組無序資料格式:OC:NSDictionary *dict = [NSDictionary dictionaryWithObject:@"lnj" forKey:@"name"];NSLog(@"%@", dict);NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"name", @"lnj", @"age", @30, nil];NSLog(@"%@", dict);NSDictionary *dict = @{@"name":@"lnj", @"age":@30};NSLog(@"%@", dict);Swift:*/// key一定要是可以hash的(String, Int, Float, Double, Bool), value沒有要求var dict0 = ["name":"lnj", "age":30]println(dict0)var dict1:Dictionary = ["name":"lnj", "age":30]println(dict1)var dict2:Dictionary<String,Any> = ["name":"lnj", "age":30]println(dict2)var dict3:[String:Any] = ["name":"lnj", "age":30]println(dict3)var dict4:[String:Any] = Dictionary(dictionaryLiteral: ("name", "lnj"), ("age", 30))println(dict4)//空字典:var dict01:Dictionary<String,Any> = [:]var dict02 = Dictionary<String,Any>()var dict03 = [String:Any]()//不可變數組:var dict:Dictionary<String,Any> = [:]//可變數組:let dict:Dictionary<String,Any> = [:]/*字典操作OC:1.擷取NSDictionary *dict = @{@"name":@"lnj", @"age":@30};NSLog(@"%@", dict[@"name"]);2.修改NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"lnj", @"age", @30, nil];dict[@"name"] = @"iversion";NSLog(@"%@", dict[@"name"]);NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"lnj", @"age", @30, nil];[dict setObject:@"iversion" forKey:@"name"];NSLog(@"%@", dict[@"name"]);Swift:*///1.擷取var dict04 = ["name":"lnj", "age":30]println(dict04["name"]!)//2.修改var dict05 = ["name":"lnj", "age":30]dict05["name"] = "iverson"println(dict05["name"]!)var dict06 = ["name":"lnj", "age":30]dict06.updateValue("iverson", forKey: "name")println(dict06["name"]!)var dict08 = ["name":"lnj", "age":30]// updateValue返回一個可選類型, 如果字典中不存在需要更新的key, 那麼返回nil, 如果存在返回原始值if let orignal = dict08.updateValue("iverson", forKey: "name"){println(dict08["name"]!)println(orignal)}var dict07 = ["name":"lnj", "age":30]// updateValue返回一個可選類型, 如果字典中不存在需要更新的key, 那麼返回nil並且會將新的索引值對添加到字典中if let orignal = dict07.updateValue("iverson", forKey: "abc"){println(dict07["abc"]!)println(orignal)}println(dict07)/*4.添加OC:NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"lnj", @"age", @30, nil];dict[@"height"] = @175;NSLog(@"%@", dict);NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"lnj", @"age", @30, nil];[dict setObject:@175 forKey:@"height"];NSLog(@"%@", dict);Swift:*/var dict09 = ["name":"lnj", "age":30]dict09["height"] = 175;println(dict09)/*5.刪除OC:NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @30, @"age", nil];[dict removeObjectForKey:@"name"];NSLog(@"%@", dict);NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @30, @"age", nil];[dict removeAllObjects];NSLog(@"%@", dict);Swift:*/var dict_1 = ["name":"lnj", "age":30]dict_1.removeValueForKey("name")println(dict_1)var dict_2 = ["name":"lnj", "age":30]// removeValueForKey返回一個可選類型, 如果字典中不存在需要刪除的key, 那麼返回nil並且不會執行任何操作, 如果存在則刪除key對應的值, 並且返回被刪除的值if let orignal = dict_2.removeValueForKey("names"){println(dict_2)println(orignal)}println(dict_2)var dict_3 = ["name":"lnj", "age":30]dict_3.removeAll(keepCapacity: true)println("====--遍曆字典--====");/*遍曆字典OC:NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @30, @"age", nil];[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {NSLog(@"key = %@ value = %@", key, obj);}];NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @30, @"age", nil];NSArray *keys = [dict allKeys];for (NSString *key in keys) {NSLog(@"%@", key);}NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @30, @"age", nil];NSArray *values = [dict allValues];for (NSString *value in values) {NSLog(@"%@", value);}Swift:*/var dict_a = ["name":"lnj", "age":30]for (key , value) in enumerate(dict_a){println("key = \(key) value = \(value)")}var dict_b = ["name":"lnj", "age":30]for key in dict_b.keys{println("key = \(key)")}var dict_c = ["name":"lnj", "age":30]for value in dict_c.values{println("value = \(value)")}var dict_d = ["name":"lnj", "age":30]for (key , value) in dict_d{println("key = \(key) value = \(value)")}

 

|--> Copyright (c) 2015 Bing Ma.

|--> GitHub RUL: https://github.com/SpongeBob-GitHub

Swift # 字典

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.