美文网首页
swift Dictionary

swift Dictionary

作者: 哎呦我去叫什么呢 | 来源:发表于2021-01-25 10:51 被阅读0次
     1. 定义固定格式的字典
    // dict1的所有“键值”类型一致(都是字符串)
    var dict1 = ["name": "mary", "age": "18"]
    
    或者
     var dataSource = [[String:String]()]
    
     dataSource = [
                      ["type":"no","name":"张三","sex":"男","icon":"my_def_photo","birthday":"2017-10-11"],
                      ["type":"no","name":"李四","sex":"男","icon":"my_def_photo","birthday":"2011-12-30"],
                      ["type":"no","name":"王五","sex":"女","icon":"my_def_photo","birthday":"2014-9-10"],
                      ["type":"no","name":"JIM","sex":"赵六","icon":"my_def_photo","birthday":"2008-10-1"]]
    
    

    修改元素

    dict1["name"] = "rose"//方法一
    dict1.updateValue("rose", forKey: "name")//方法二
    

    获取数据

    let type = dict["type"]
    或
    let dict:Dictionary = dataSource[1]
    let type = dict["type"]
    

    删除某一个键值

    dict1.removeValueForKey("type")
    

    添加新的键值对

    dict1["height"] = "1.65M"
    

    相关文章

      网友评论

          本文标题:swift Dictionary

          本文链接:https://www.haomeiwen.com/subject/ytubzktx.html