//MARK:----------字典--------------/* 字典(key : value)*///如果类型信息已知,使用[:]声明空字典。let emptyDictionary1 = [:]let emptyDictionary2 = Dictionary<String,float>()let emptyDictionary3 = [String: float]()print(emptyDictionary2.count)//var airports: Dictionary<String,String> = ["TYO": "Tokyo","dub": "dublin"]var airportsDic = ["TYO": "Tokyo","dub": "dublin"]print(airportsDic["TYO"])//---------添加----------airportsDic["LHR"] = "London"print(airportsDic)//-------修改------airportsDic["LHR"] = "London Heathrow" // the value for "LHR" has been changed to "London Heathrowprint(airportsDic)//等价于//airports.updateValue("dublin International",forKey: "dub")//print(airports)//------------删除-----------airportsDic["LHR"] = nilprint(airportsDic)//等价于//airportsDic("dub")//print(airportsDic)//---------查询----------print("字典元素个数为" + String(airportsDic.count))print("字典包含 \(airportsDic.count) 个元素.")print(airportsDic.startIndex)print(airportsDic.endindex)//-------------遍历字典-----------for (key,Value) in airportsDic{ print("\(key): \(Value)")}for key in airportsDic.keys{ print("Airport code: \(key)")}for Value in airportsDic.values{ print("Airport name: \(Value)")}let airportKeys = Array(airportsDic.keys) // airportCodes is ["TYO","LHR"]let airportValues = Array(airportsDic.values) // airportnames is ["Tokyo","London Heathrow"]print(airportKeys)print(airportValues)总结
以上是内存溢出为你收集整理的Swift教程之NSDictionary全部内容,希望文章能够帮你解决Swift教程之NSDictionary所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)