Swift教程之NSDictionary
发布时间:2020-12-14 02:02:40 所属栏目:百科 来源:网络整理
导读://MARK:----------字典--------------/* 字典(key : value)*///如果类型信息已知,使用[:]声明空字典。let emptyDictionary1 = [:]let emptyDictionary2 = DictionaryString,Float()let emptyDictionary3 = [String: Float]()print(emptyDictionary2.count
//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 Heathrow print(airportsDic) //等价于 //airports.updateValue("Dublin International",forKey: "DUB") //print(airports) //------------删除----------- airportsDic["LHR"] = nil print(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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |