這裡講述的是Swift_字典詳解的文章,具體方法請看介紹
Swift_字典詳解點擊查看源碼
初始化fileprivate func testInit() {
//空字典
var dic = [String:String]()
print(dic)
dic = [:]
print(dic)
dic = Dictionary()
print(dic)
dic = Dictionary<String,String>()
print(dic)
//非空字典
dic = ["name":"XuBaoAiChiYu","QQ":"1045214799"]
print(dic)
dic = Dictionary(dictionaryLiteral: ("name","XuBaoAiChiYu"),("QQ","1045214799"))
print(dic)
/* print
[:]
[:]
[:]
[:]
["name": "XuBaoAiChiYu", "QQ": "1045214799"]
["name": "XuBaoAiChiYu", "QQ": "1045214799"]
*/
}
相關屬性
//相關屬性
fileprivate func testProperties() {
let dic = ["name":"XuBaoAiChiYu","QQ":"1045214799"]
print(dic)
print("count:\(dic.count)") // 有多個對元素
print("isEmpty:\(dic.isEmpty)") // 是否為空
// 獲取所有key
let keys = [String](dic.keys)
print("keys:\(keys)")
// 獲取所有value
let values = [String](dic.values)
print("values:\(values)")
print("startIndex:\(dic.startIndex)") // 首腳標
print("endIndex:\(dic.endIndex)") // 尾腳標
/* print
["name": "XuBaoAiChiYu", "QQ": "1045214799"]
count:2
isEmpty:false
keys:["name", "QQ"]
values:["XuBaoAiChiYu", "1045214799"]
startIndex:DictionaryIndex<String, String>(_value: Swift.DictionaryIndexRepresentation<Swift.String, Swift.String>._native(Swift._NativeDictionaryIndex<Swift.String, Swift.String>(nativeStorage: , offset: 1)))
endIndex:DictionaryIndex<String, String>(_value: Swift.DictionaryIndexRepresentation<Swift.String, Swift.String>._native(Swift._NativeDictionaryIndex<Swift.String, Swift.String>(nativeStorage: , offset: 4)))
*/
}
查找元素
//查找元素
fileprivate func testFindingObjects() {
let dic = ["name":"XuBaoAiChiYu","QQ":"1045214799"]
print(dic)
if let dicIndex = dic.index(forKey: "name") { // 獲取位置
let item = dic[dicIndex] // 根據位置獲取鍵值對
print("key:\(item.0);value:\(item.1)")
}
let value = dic["name"] // 根據key提取Value
print("value:\(value)")
// 遍歷輸出
for (key, value) in dic {
print("\(key): \(value)")
}
// 所有key輸出
for key in dic.keys {
print("key:\(key)")
}
// 所有value輸出
for value in dic.values {
print("value: \(value)")
}
/* print
["name": "XuBaoAiChiYu", "QQ": "1045214799"]
key:name;value:XuBaoAiChiYu
value:Optional("XuBaoAiChiYu")
name: XuBaoAiChiYu
QQ: 1045214799
key:name
key:QQ
value: XuBaoAiChiYu
value: 1045214799
*/
}
增加元素
//增加元素
fileprivate func testAdd() {
var dic = ["name":"XuBaoAiChiYu","QQ":"1045214799"]
print(dic)
// 如果沒有則添加,有則修改
dic["language"] = "swift"
print(dic)
/* print
["name": "XuBaoAiChiYu", "QQ": "1045214799"]
["language": "swift", "name": "XuBaoAiChiYu", "QQ": "1045214799"]
*/
}
刪除元素
//刪除元素
fileprivate func testRemove() {
var dic = ["name":"XuBaoAiChiYu","QQ":"1045214799"]
print(dic)
let oldValue = dic.removeValue(forKey: "name")// 根據key刪除,並返回刪除的value
print("\(oldValue)")
// 先找到位置,然後根據位置刪除
if let dictIndex = dic.index(forKey: "QQ") {
let oldItem = dic.remove(at: dictIndex)// 返回刪除的元素對
print("key:\(oldItem.0);value:\(oldItem.1)")
}
dic.removeAll() // 刪除所有元素
print(dic)
/* print
["name": "XuBaoAiChiYu", "QQ": "1045214799"]
Optional("XuBaoAiChiYu")
key:QQ;value:1045214799
[:]
*/
}
修改元素
//修改元素
fileprivate func testReplace() {
var dic = ["name":"XuBaoAiChiYu","QQ":"1045214799"]
print(dic)
// 如果沒有則添加,有則修改
dic["name"] = "XUBAO" // 修改
print(dic)
let oldValue = dic.updateValue("XUBAOAICHIYU", forKey: "name") // 修改,並返回原來的value
print("\(oldValue)")
/* print
["name": "XuBaoAiChiYu", "QQ": "1045214799"]
["name": "XUBAO", "QQ": "1045214799"]
Optional("XUBAO")
*/
}
排序
//排序
fileprivate func testSort() {
let dic = ["name":"XuBaoAiChiYu","QQ":"1045214799"]
print(dic)
// 排序主要只排序key或者value,然後借用Array的排序
// 排序key
var array = dic.keys.sorted(by: { str1,str2 in str1 < str2 })
array = dic.keys.sorted(by: <)
print(array)
// 排序value
array = dic.values.sorted(by: {$0 > $1})
print(array)
/* print
["name": "XuBaoAiChiYu", "QQ": "1045214799"]
["QQ", "name"]
["XuBaoAiChiYu", "1045214799"]
*/
}
文件讀寫
fileprivate func testReadingAndWriting() {
// Document目錄
let documents:[String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let docDirPath = documents.first!
let path = (docDirPath as NSString).appendingPathComponent("test.plist")
let url = URL(fileURLWithPath: path)
var dic = ["name":"XuBaoAiChiYu","QQ":"1045214799"]
// 寫
(dic as NSDictionary).write(toFile: path, atomically: true)
(dic as NSDictionary).write(to: url, atomically: true)
// 讀
dic = NSDictionary(contentsOfFile: path) as! Dictionary
print(dic)
dic = NSDictionary(contentsOf: url) as! Dictionary
print(dic)
/* print
["name": "XuBaoAiChiYu", "QQ": "1045214799"]
["name": "XuBaoAiChiYu", "QQ": "1045214799"]
*/
}
通本學習您是不是更了解IOS開發了呢.感謝關注本站
【Swift_字典詳解】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!