美文网首页
swift中的字典

swift中的字典

作者: JackMayx | 来源:发表于2016-03-25 16:46 被阅读36次

定义一个字典:

var dict = Dictionary<Key类型,value类型>()  ——空字典!

或者  var dity:Dictionary= [“key1”:“www”,“key2”:“aaaa”,“key3”:“dddd”]

1.给字典中存储数据

dict.updateValue(value: Value, forKey: Key)

dict[2] = "B";

dict[3] = "C";

2.移除字典中的数据

dict.removeAll();

dict.removeAll(keepCapacity: true);

3.通过Key查找value元素

  dict[Key]

4.遍历字典

///遍历字典的Key和Value

for (key,value) in dict {

    print("\(key): \(value)")

}

///遍历字典的Key

for dictKeys in dict.keys {

    print("dict.keys: \(dictKeys)")

}

///遍历字典的Value

for dictValues in dict.values {

  print("dict.values: \(dictValues)")

}

相关文章

网友评论

      本文标题:swift中的字典

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