1.Dictionary (可变不可变不区分了)字典 《Swift开发指南》
// Swift 字典的key没有类型限制可以是整型或字符串,但必须是唯一的。
var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
//(1)创建一个字典
var dic:Dictionary<String,String>=["三国演义":"罗贯中","水浒传":"施耐庵","红楼梦":"曹雪芹"];
//(2)向字典中添加键值 "西记":"吴承恩"
dic["吴承恩”]="西记"
//(3)修改
dic.updateValue("西游记",forKey:"吴承恩")
//(4)删除
dic.removeValueForKey("吴承恩")
//(5)遍历 for in 字典 会以元组(键,值)的形式返回
for (key,value) in dic {
print("键:\(key) 值:\(value)");
}
// 元组
for tuples in dic {
print("键:\(tuples.0) 值:\(tuples.1)");
}
for key in dic.keys
{
print("key=:\(key)");
}
for value in dic.values
{
print("value=:\(value)");
}
//(6)通过构造方法创建字典
var namesOfIntegers = Dictionary<String, String>()
namesOfIntegers = dic
print("\(namesOfIntegers)")
/*******************字典的基本使用**************************/
// Swift 字典的key没有类型限制可以是整型或字符串,但必须是唯一的。
var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var removedValue = someDict.removeValue(forKey: 2)
print( "key = 1 的值为 \(someDict[1]!)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )
let testMutable:Bool = false
if !testMutable {
// 可变字典的基本使用
let mutable : NSMutableDictionary = NSMutableDictionary()
// add objects
mutable.setObject("Tom", forKey: "tom@jones.com" as NSCopying)
mutable.setObject("Bob", forKey: "bob@dole.com" as NSCopying)
mutable["lucy@163.com"] = "Lucy"
print("可变字典-----%@", mutable.description)
let keys = mutable.allKeys
for item : Any in keys {
let key = item as! String
print("%@ - %@", key, mutable.object(forKey: key) as! NSString)
}
}else {
// 不变字典的基本使用
let keyString : String = "one two three four five"
let keys : Array = keyString.components(separatedBy: " ")
let valuestring : String = "alpha bravo charlie delta echo"
let values : Array = valuestring.components(separatedBy: " ")
let dict : NSDictionary = NSDictionary(objects: values as [AnyObject], forKeys:keys as [NSCopying] )
print("******dict********\(dict)")
let value:NSString = dict.object(forKey: "three") as! NSString
NSLog("three = %@", value)
let dict1 : NSDictionary = NSDictionary.init(object: values, forKey: keys as NSCopying)
print("*******dict1*******\(dict1)")
let keys1 = dict.allKeys
for item : Any in keys1 {
let key = item as! NSString
print("%@ - %@", key, dict.object(forKey: key) as! NSString)
}
}
/******************Dictionary*************/
var submitDic:Dictionary<String,String>=["userName":"muyu07","password":"123456"]
submitDic.updateValue("MUYU07", forKey: "userName")
submitDic["inviteCode"] = "CJ10001"
print(submitDic)
// 三、NSDictionary与Dictionary之间的关系
// NSDictionary与Dictionary之间的关系如同NSArray与Array之间的关系,Swift在底层能够将它们自动地桥接起来,一个NSDictionary对象桥接之后的结果是[NSObject : AnyObject]字典(值为NSObject 类型,键为AnyObject 类型的Dictionary字典)。
let keyString1 : NSString = "one two three four five"
let keys1 : Array = keyString1.components(separatedBy: " ")
let valueString1 : NSString = "alpha bravo charlie delta echo"
let values1 : Array = valueString1.components(separatedBy: " ")
let foundationDict:NSDictionary = NSDictionary(objects:values1 as [AnyObject],forKeys:
keys1 as [NSCopying])
let swiftDict:Dictionary = foundationDict as Dictionary
print("swiftDict.description-----\(swiftDict.description)")
let keys3 = swiftDict.keys
for item : AnyObject in keys3 {
let key = item as! NSString
print("key:%@ --value:%@",key,swiftDict[key]);
}
var hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]
print(hues["Coral"])
// Prints "Optional(16)"
print(hues["Cerise"])
//************** 概 览 **************
let swiftDict001 :Dictionary<String,String>= ["key1":"value1","key2":"value2"]
let nsDict001 :NSDictionary = swiftDict001 as NSDictionary
nsDict001.allKeys //["key1", "key2"]
nsDict001.allKeys(for: "value1") //["key1"]
nsDict001.allValues //["value1", "value2"]
//NSDictionary转换为Dictionary后,在输出时需要指定成员类型为AnyObject类型--不指定,Swift也能识别
let swiftDict002 = nsDict001 as Dictionary
for (key2, value2)in swiftDict002 {
print("\(key2):\(value2)")
}
for (key, value)in swiftDict002 {
print("\(key):\(value)")
}
//========== NSDictionary初始化 ==========
let nsDict003 = NSDictionary(object:"value1", forKey:001 as NSCopying)
let nsDict004 = NSDictionary(objects: ["value1","value2"], forKeys: [001 as NSCopying,002 as NSCopying])//一次性创建多个
let swiftDict003 = ["A":"value1","C":"value3","B":"value2"]
let nsDict005 = NSDictionary(dictionary:swiftDict003)
//========== NSDictionary常用的属性和方法 ==========
//获得成员个数
swiftDict002.count
nsDict001.count
//获取Key或Value组成的数组(swift Dictionary无此方法)
nsDict001.allKeys
nsDict001.allValues
//使用keysSortedByValueUsingComparator方法将key排序,获得新数组(swiftDictionary无此方法)
let sortKeys = nsDict005.keysSortedByValue(comparator: {($0 as! String).compare($1 as! String)})
print("\(sortKeys)") //[A, B, C]---由小到大排序
//获得单一键值(swift Dictionary无此方法)
nsDict005.object(forKey: "A")! //"value1"
nsDict005["A"]! //"value1"
swiftDict003["A"]
//获得多个键值(swift Dictionary无此方法)
nsDict005.objects(forKeys: ["A","D","B"],notFoundMarker:"NotFound") //["value1","NotFound", "value2"]
swiftDict003["A"]