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"
网友评论