• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift_字典详解

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

点击查看源码

初始化

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"]
     
     */
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift-技巧(十一)重写运算符发布时间:2022-07-13
下一篇:
swift类方法(+),实例方法(-),convenience(便利构造函数)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap