【Swift4.0】集合类型-Dictionary

翻译能力有限,如有不对的地方,还请见谅!希望对Swift的学习者有所帮助微笑,使用的编写工具:JQNote



Dictionary是一个字典,存储的是键值对,也是无序的。每一个值对应唯一的key。写作Dictionary<Key, Value>, 其中key是Dictionary中一个键的类型,Value是Dictionary中与键对应的存储值的类型。Dictionary简写形式为[Key : Value]


与集合类型Set类似,Dictionary中的Key类型必须遵守Hashable协议


创建一个空的Dictionary

跟Array一样,你可以使用初始化语法创建一个空Dictionary:

var namesOfIntegers = [Int :String] ()


这个例子创建了一个空的[Int : String]字典类型,它的Key是Int类型,Value是String类型。如果代码上下文已经提供了类型信息,那么可以创建一个空的Dictionary,使用[ : ].


namesOfIntegers[16] = “Sixteen


namesOfIntegers = [:].


也可以这样创建一个Dictionary:


var airports: [String:String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]


airports声明为一个[String:String]类型的Dictionary,也就是说,它的key是String类型,它的Value也是String类型。


与Array一样,如果初始化值的类型是明确的,那么也可以不用写[String: String]:

var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]


因为初始化值每一个key都是String,对应每个Value也都是String,Swift会自动推断该Dictionary的类型为[String:String].


获取和修改Dictionary


跟Array一样,你可以使用Dictionary的只读属性count来检查它的存储项数量

print("The airports dictionary contains \(airports.count) items.")

// Prints "The airports dictionary contains 2 items.


使用isEmpty属性来判断是否count属性为0

if airports.isEmpty {

    print("The airports dictionary is empty.")

} else {

    print("The airports dictionary is not empty.")

}

// Prints "The airports dictionary is not empty.”


使用下标索引语法来添加一个合适类型的key以及对应的值,

airports["LHR"] ="London"


或者改变一个值

airports["LHR"] ="London Heathrow” “// the value for "LHR" has been changed to "London Heathrow”


也可以使用updateValue(_:forKey:)方法来更新某个特定key对应的值, 如果不存在,会添加一个,如果存在,则更新。并且返回原来的旧值。

if letoldValue = airports.updateValue("Dublin Airport",forKey: "DUB") {

    print("The old value for DUB was \(oldValue).")

}

// Prints "The old value for DUB was Dublin.”


也可以使用下标索引:

if let airportName= airports["DUB"]{

    print("The name of the airport is\(airportName).")

} else {

    print("That airport is not in the airports dictionary.")

}

// Prints "The name of the airport is Dublin Airport.”


删除一个键值对:

airports["APL"]= "Apple International"

// "Apple International" is not the real airport for APL, so delete it

airports["APL"] =nil

// APL has now been removed from the dictionary”


或者使用removeValue(forKey:) 

if letremovedValue = airports.removeValue(forKey:"DUB") {

    print("The removed airport's name is\(removedValue).")

} else {

    print("The airports dictionary does not contain a value for DUB.")

}

// Prints "The removed airport's name is Dublin Airport.”


遍历Dictionary

可以使用for-in循环来遍历一个Dictionary,每一项返回一个(key,value)元组


for (airportCode,airportName) inairports {

    print("\(airportCode): \(airportName)")

}

// YYZ: Toronto Pearson

// LHR: London Heathrow



如果想获取Dictionary中所有的key,或者所有的value,可以使用它的keys和values属性:

let airportCodes= [String](airports.keys)

// airportCodes is ["YYZ", "LHR"]

 

let airportNames= [String](airports.values)

// airportNames is ["Toronto Pearson", "London Heathrow"]


最后,Dictionary是无序的,如果想有序的遍历一个Dictionary,用sorted()方法在它的keys和values属性。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值