Swift-2.4闆嗗悎绫诲瀷

鏈〉鍖呭惈鍐呭锛�/p>

  • 闆嗗悎鐨勫彲鍙樻�锛圡utability of Collections锛�/a>
  • 鏁扮粍锛圓rrays锛�/a>
  • 闆嗗悎锛圫ets锛�/a>
  • 瀛楀吀锛圖ictionaries锛�/a>

Swift 璇█鎻愪緵Arrays銆�code>Sets鍜�code>Dictionaries涓夌鍩烘湰鐨勯泦鍚堢被鍨嬬敤鏉ュ瓨鍌ㄩ泦鍚堟暟鎹�鏁扮粍锛圓rrays锛夋槸鏈夊簭鏁版嵁鐨勯泦銆傞泦鍚堬紙Sets锛夋槸鏃犲簭鏃犻噸澶嶆暟鎹殑闆嗐�瀛楀吀锛圖ictionaries锛夋槸鏃犲簭鐨勯敭鍊煎鐨勯泦銆�/p>

Swift 璇█涓殑Arrays銆�code>Sets鍜�code>Dictionaries涓瓨鍌ㄧ殑鏁版嵁鍊肩被鍨嬪繀椤绘槑纭�杩欐剰鍛崇潃鎴戜滑涓嶈兘鎶婁笉姝g‘鐨勬暟鎹被鍨嬫彃鍏ュ叾涓�鍚屾椂杩欎篃璇存槑鎴戜滑瀹屽叏鍙互瀵瑰彇鍥炲�鐨勭被鍨嬮潪甯歌嚜淇°�

娉ㄦ剰锛�br />Swift 鐨�code>Arrays銆�code>Sets鍜�code>Dictionaries绫诲瀷琚疄鐜颁负娉涘瀷闆嗗悎銆傛洿澶氬叧浜庢硾鍨嬬被鍨嬪拰闆嗗悎锛屽弬瑙�nbsp;娉涘瀷绔犺妭銆�/p>

闆嗗悎鐨勫彲鍙樻�

濡傛灉鍒涘缓涓�釜Arrays銆�code>Sets鎴�code>Dictionaries骞朵笖鎶婂畠鍒嗛厤鎴愪竴涓彉閲忥紝杩欎釜闆嗗悎灏嗕細鏄�em>鍙彉鐨�/em>銆傝繖鎰忓懗鐫�垜浠彲浠ュ湪鍒涘缓涔嬪悗娣诲姞鏇村鎴栫Щ闄ゅ凡瀛樺湪鐨勬暟鎹」锛屾垨鑰呮敼鍙橀泦鍚堜腑鐨勬暟鎹」銆傚鏋滄垜浠妸Arrays銆�code>Sets鎴�code>Dictionaries鍒嗛厤鎴愬父閲忥紝閭d箞瀹冨氨鏄�em>涓嶅彲鍙樼殑锛屽畠鐨勫ぇ灏忓拰鍐呭閮戒笉鑳借鏀瑰彉銆�/p>

娉ㄦ剰锛�br />鍦ㄦ垜浠笉闇�鏀瑰彉闆嗗悎鐨勬椂鍊欏垱寤轰笉鍙彉闆嗗悎鏄緢濂界殑瀹炶返銆傚姝�Swift 缂栬瘧鍣ㄥ彲浠ヤ紭鍖栨垜浠垱寤虹殑闆嗗悎銆�/p>

鏁扮粍(Arrays)

鏁扮粍浣跨敤鏈夊簭鍒楄〃瀛樺偍鍚屼竴绫诲瀷鐨勫涓�銆傜浉鍚岀殑鍊煎彲浠ュ娆″嚭鐜板湪涓�釜鏁扮粍鐨勪笉鍚屼綅缃腑銆�/p>

娉ㄦ剰: Swift 鐨�code>Array绫诲瀷琚ˉ鎺ュ埌Foundation涓殑NSArray绫汇� 鏇村鍏充簬鍦�code>Foundation鍜�code>Cocoa涓娇鐢�code>Array鐨勪俊鎭紝鍙傝 Using Swift with Cocoa and Obejective-C 涓�功銆�/p>

var someInts = [Int]() print("someInts is of type [Int] with \(someInts.count) items.") // 鎵撳嵃 "someInts is of type [Int] with 0 items."

娉ㄦ剰锛岄�杩囨瀯閫犲嚱鏁扮殑绫诲瀷锛�code>someInts鐨勫�绫诲瀷琚帹鏂负[Int]銆�/p>

鎴栬�锛屽鏋滀唬鐮佷笂涓嬫枃涓凡缁忔彁渚涗簡绫诲瀷淇℃伅锛屼緥濡備竴涓嚱鏁板弬鏁版垨鑰呬竴涓凡缁忓畾涔夊ソ绫诲瀷鐨勫父閲忔垨鑰呭彉閲忥紝鎴戜滑鍙互浣跨敤绌烘暟缁勮鍙ュ垱寤轰竴涓┖鏁扮粍锛屽畠鐨勫啓娉曞緢绠�崟锛�code>[]锛堜竴瀵圭┖鏂规嫭鍙凤級锛�/p>

someInts.append(3) // someInts 鐜板湪鍖呭惈涓�釜 Int 鍊� someInts = [] // someInts 鐜板湪鏄┖鏁扮粍锛屼絾鏄粛鐒舵槸 [Int] 绫诲瀷鐨勩� 

鍒涘缓涓�釜甯︽湁榛樿鍊肩殑鏁扮粍

Swift 涓殑Array绫诲瀷杩樻彁渚涗竴涓彲浠ュ垱寤虹壒瀹氬ぇ灏忓苟涓旀墍鏈夋暟鎹兘琚粯璁ょ殑鏋勯�鏂规硶銆傛垜浠彲浠ユ妸鍑嗗鍔犲叆鏂版暟缁勭殑鏁版嵁椤规暟閲忥紙count锛夊拰閫傚綋绫诲瀷鐨勫垵濮嬪�锛�code>repeatedValue锛変紶鍏ユ暟缁勬瀯閫犲嚱鏁帮細

var threeDoubles = [Double](count: 3, repeatedValue:0.0) // threeDoubles 鏄竴绉�[Double] 鏁扮粍锛岀瓑浠蜂簬 [0.0, 0.0, 0.0] 

閫氳繃涓や釜鏁扮粍鐩稿姞鍒涘缓涓�釜鏁扮粍

鎴戜滑鍙互浣跨敤鍔犳硶鎿嶄綔绗︼紙+锛夋潵缁勫悎涓ょ宸插瓨鍦ㄧ殑鐩稿悓绫诲瀷鏁扮粍銆傛柊鏁扮粍鐨勬暟鎹被鍨嬩細琚粠涓や釜鏁扮粍鐨勬暟鎹被鍨嬩腑鎺ㄦ柇鍑烘潵锛�/p>

var anotherThreeDoubles = Array(count: 3, repeatedValue: 2.5) // anotherThreeDoubles 琚帹鏂负 [Double]锛岀瓑浠蜂簬 [2.5, 2.5, 2.5] var sixDoubles = threeDoubles + anotherThreeDoubles // sixDoubles 琚帹鏂负 [Double]锛岀瓑浠蜂簬 [0.0, 0.0, 0.0, 2.5, 2.5, 2.5] 

鐢ㄥ瓧闈㈤噺鏋勯�鏁扮粍

鎴戜滑鍙互浣跨敤瀛楅潰閲忔潵杩涜鏁扮粍鏋勯�锛岃繖鏄竴绉嶇敤涓�釜鎴栬�澶氫釜鏁板�鏋勯�鏁扮粍鐨勭畝鍗曟柟娉曘�瀛楅潰閲忔槸涓�郴鍒楃敱閫楀彿鍒嗗壊骞剁敱鏂规嫭鍙峰寘鍚殑鏁板�锛�/p>

[value 1, value 2, value 3]銆�/p>

涓嬮潰杩欎釜渚嬪瓙鍒涘缓浜嗕竴涓彨鍋�code>shoppingList骞朵笖瀛樺偍String鐨勬暟缁勶細

var shoppingList: [String] = ["Eggs", "Milk"] // shoppingList 宸茬粡琚瀯閫犲苟涓旀嫢鏈変袱涓垵濮嬮」銆� 

shoppingList鍙橀噺琚0鏄庝负“瀛楃涓插�绫诲瀷鐨勬暟缁�ldquo;锛岃浣�code>[String]銆�鍥犱负杩欎釜鏁扮粍琚瀹氬彧鏈�code>String涓�鏁版嵁缁撴瀯锛屾墍浠ュ彧鏈�code>String绫诲瀷鍙互鍦ㄥ叾涓瀛樺彇銆�鍦ㄨ繖閲岋紝shoppinglist鏁扮粍鐢变袱涓�code>String鍊硷紙"Eggs" 鍜�code>"Milk"锛夋瀯閫狅紝骞朵笖鐢卞瓧闈㈤噺瀹氫箟銆�/p>

娉ㄦ剰锛�br />Shoppinglist鏁扮粍琚0鏄庝负鍙橀噺锛�code>var鍏抽敭瀛楀垱寤猴級鑰屼笉鏄父閲忥紙let鍒涘缓锛夋槸鍥犱负浠ュ悗鍙兘浼氭湁鏇村鐨勬暟鎹」琚彃鍏ュ叾涓�

鍦ㄨ繖涓緥瀛愪腑锛屽瓧闈㈤噺浠呬粎鍖呭惈涓や釜String鍊笺�鍖归厤浜嗚鏁扮粍鐨勫彉閲忓0鏄庯紙鍙兘鍖呭惈String鐨勬暟缁勶級锛屾墍浠ヨ繖涓瓧闈㈤噺鐨勫垎閰嶈繃绋嬪彲浠ヤ綔涓虹敤涓や釜鍒濆椤规潵鏋勯�shoppinglist鐨勪竴绉嶆柟寮忋�

鐢变簬 Swift 鐨勭被鍨嬫帹鏂満鍒讹紝褰撴垜浠敤瀛楅潰閲忔瀯閫犲彧鎷ユ湁鐩稿悓绫诲瀷鍊兼暟缁勭殑鏃跺�锛屾垜浠笉蹇呮妸鏁扮粍鐨勭被鍨嬪畾涔夋竻妤氥� shoppinglist鐨勬瀯閫犱篃鍙互杩欐牱鍐欙細

var shoppingList = ["Eggs", "Milk"] 

鍥犱负鎵�湁瀛楅潰閲忎腑鐨勫�閮芥槸鐩稿悓鐨勭被鍨嬶紝Swift 鍙互鎺ㄦ柇鍑�code>[String]鏄�code>shoppinglist涓彉閲忕殑姝g‘绫诲瀷銆�/p>

print("The shopping list contains \(shoppingList.count) items.") // 杈撳嚭 "The shopping list contains 2 items."锛堣繖涓暟缁勬湁2涓」锛�

浣跨敤甯冨皵鍊煎睘鎬�code>isEmpty浣滀负妫�煡count灞炴�鐨勫�鏄惁涓�0 鐨勬嵎寰勶細

if shoppingList.isEmpty { print("The shopping list is empty.") } else { print("The shopping list is not empty.") } // 鎵撳嵃 "The shopping list is not empty."锛坰hoppinglist 涓嶆槸绌虹殑锛� 

涔熷彲浠ヤ娇鐢�code>append(_:)鏂规硶鍦ㄦ暟缁勫悗闈㈡坊鍔犳柊鐨勬暟鎹」锛�/p>

shoppingList.append("Flour") // shoppingList 鐜板湪鏈�涓暟鎹」锛屾湁浜哄湪鎽婄厧楗� 

闄ゆ涔嬪锛屼娇鐢ㄥ姞娉曡祴鍊艰繍绠楃锛�code>+=锛変篃鍙互鐩存帴鍦ㄦ暟缁勫悗闈㈡坊鍔犱竴涓垨澶氫釜鎷ユ湁鐩稿悓绫诲瀷鐨勬暟鎹」锛�/p>

shoppingList += ["Baking Powder"] // shoppingList 鐜板湪鏈夊洓椤逛簡 shoppingList += ["Chocolate Spread", "Cheese", "Butter"] // shoppingList 鐜板湪鏈変竷椤逛簡 

鍙互鐩存帴浣跨敤涓嬫爣璇硶鏉ヨ幏鍙栨暟缁勪腑鐨勬暟鎹」锛屾妸鎴戜滑闇�鐨勬暟鎹」鐨勭储寮曞�鏀惧湪鐩存帴鏀惧湪鏁扮粍鍚嶇О鐨勬柟鎷彿涓細

var firstItem = shoppingList[0] // 绗竴椤规槸 "Eggs" 

娉ㄦ剰锛�br />绗竴椤瑰湪鏁扮粍涓殑绱㈠紩鍊兼槸0鑰屼笉鏄�code>1銆�Swift 涓殑鏁扮粍绱㈠紩鎬绘槸浠庨浂寮�銆�/p>

鎴戜滑涔熷彲浠ョ敤涓嬫爣鏉ユ敼鍙樻煇涓凡鏈夌储寮曞�瀵瑰簲鐨勬暟鎹�锛�/p>

shoppingList[0] = "Six eggs" // 鍏朵腑鐨勭涓�」鐜板湪鏄�"Six eggs" 鑰屼笉鏄�"Eggs" 

杩樺彲浠ュ埄鐢ㄤ笅鏍囨潵涓�鏀瑰彉涓�郴鍒楁暟鎹�锛屽嵆浣挎柊鏁版嵁鍜屽師鏈夋暟鎹殑鏁伴噺鏄笉涓�牱鐨勩�涓嬮潰鐨勪緥瀛愭妸"Chocolate Spread"锛�code>"Cheese"锛屽拰"Butter"鏇挎崲涓�code>"Bananas"鍜�nbsp;"Apples"锛�/p>

shoppingList[4...6] = ["Bananas", "Apples"] // shoppingList 鐜板湪鏈�椤� 

娉ㄦ剰锛�br />涓嶅彲浠ョ敤涓嬫爣璁块棶鐨勫舰寮忓幓鍦ㄦ暟缁勫熬閮ㄦ坊鍔犳柊椤广�

璋冪敤鏁扮粍鐨�code>insert(_:atIndex:)鏂规硶鏉ュ湪鏌愪釜鍏蜂綋绱㈠紩鍊间箣鍓嶆坊鍔犳暟鎹」锛�/p>

shoppingList.insert("Maple Syrup", atIndex: 0) // shoppingList 鐜板湪鏈�椤� // "Maple Syrup" 鐜板湪鏄繖涓垪琛ㄤ腑鐨勭涓�」 

杩欐insert(_:atIndex:)鏂规硶璋冪敤鎶婂�涓�code>"Maple Syrup"鐨勬柊鏁版嵁椤规彃鍏ュ垪琛ㄧ殑鏈�紑濮嬩綅缃紝骞朵笖浣跨敤0浣滀负绱㈠紩鍊笺�

绫讳技鐨勬垜浠彲浠ヤ娇鐢�code>removeAtIndex(_:)鏂规硶鏉ョЩ闄ゆ暟缁勪腑鐨勬煇涓�」銆傝繖涓柟娉曟妸鏁扮粍鍦ㄧ壒瀹氱储寮曞�涓瓨鍌ㄧ殑鏁版嵁椤圭Щ闄ゅ苟涓旇繑鍥炶繖涓绉婚櫎鐨勬暟鎹」锛堟垜浠笉闇�鐨勬椂鍊欏氨鍙互鏃犺瀹冿級锛�/p>

let mapleSyrup = shoppingList.removeAtIndex(0) // 绱㈠紩鍊间负0鐨勬暟鎹」琚Щ闄� // shoppingList 鐜板湪鍙湁6椤癸紝鑰屼笖涓嶅寘鎷�Maple Syrup // mapleSyrup 甯搁噺鐨勫�绛変簬琚Щ闄ゆ暟鎹」鐨勫� "Maple Syrup" 

娉ㄦ剰锛�br />濡傛灉鎴戜滑璇曠潃瀵圭储寮曡秺鐣岀殑鏁版嵁杩涜妫�储鎴栬�璁剧疆鏂板�鐨勬搷浣滐紝浼氬紩鍙戜竴涓繍琛屾湡閿欒銆傛垜浠彲浠ヤ娇鐢ㄧ储寮曞�鍜屾暟缁勭殑count灞炴�杩涜姣旇緝鏉ュ湪浣跨敤鏌愪釜绱㈠紩涔嬪墠鍏堟楠屾槸鍚︽湁鏁堛�闄や簡褰�code>count绛変簬 0 鏃讹紙璇存槑杩欐槸涓┖鏁扮粍锛夛紝鏈�ぇ绱㈠紩鍊间竴鐩存槸count - 1锛屽洜涓烘暟缁勯兘鏄浂璧风储寮曘�

鏁版嵁椤硅绉婚櫎鍚庢暟缁勪腑鐨勭┖鍑洪」浼氳鑷姩濉ˉ锛屾墍浠ョ幇鍦ㄧ储寮曞�涓�code>0鐨勬暟鎹」鐨勫�鍐嶆绛変簬"Six eggs"锛�/p>

firstItem = shoppingList[0] // firstItem 鐜板湪绛変簬 "Six eggs" 

濡傛灉鎴戜滑鍙兂鎶婃暟缁勪腑鐨勬渶鍚庝竴椤圭Щ闄わ紝鍙互浣跨敤removeLast()鏂规硶鑰屼笉鏄�code>removeAtIndex(_:)鏂规硶鏉ラ伩鍏嶆垜浠渶瑕佽幏鍙栨暟缁勭殑count灞炴�銆傚氨鍍忓悗鑰呬竴鏍凤紝鍓嶈�涔熶細杩斿洖琚Щ闄ょ殑鏁版嵁椤癸細

let apples = shoppingList.removeLast() // 鏁扮粍鐨勬渶鍚庝竴椤硅绉婚櫎浜� // shoppingList 鐜板湪鍙湁5椤癸紝涓嶅寘鎷�cheese // apples 甯搁噺鐨勫�鐜板湪绛変簬 "Apples" 瀛楃涓� 

for item in shoppingList { print(item) } // Six eggs // Milk // Flour // Baking Powder // Bananas

濡傛灉鎴戜滑鍚屾椂闇�姣忎釜鏁版嵁椤圭殑鍊煎拰绱㈠紩鍊硷紝鍙互浣跨敤enumerate()鏂规硶鏉ヨ繘琛屾暟缁勯亶鍘嗐�enumerate()杩斿洖涓�釜鐢辨瘡涓�釜鏁版嵁椤圭储寮曞�鍜屾暟鎹�缁勬垚鐨勫厓缁勩�鎴戜滑鍙互鎶婅繖涓厓缁勫垎瑙f垚涓存椂甯搁噺鎴栬�鍙橀噺鏉ヨ繘琛岄亶鍘嗭細

for (index, value) in shoppingList.enumerate() { print("Item \(String(index + 1)): \(value)") } // Item 1: Six eggs // Item 2: Milk // Item 3: Flour // Item 4: Baking Powder // Item 5: Bananas 

鏇村鍏充簬for-in寰幆鐨勪粙缁嶈鍙傝for 寰幆銆�/p>

Using Swift with Cocoa and Objective-C銆�/p>

闆嗗悎绫诲瀷鐨勫搱甯屽�

涓�釜绫诲瀷涓轰簡瀛樺偍鍦ㄩ泦鍚堜腑锛岃绫诲瀷蹇呴』鏄彲鍝堝笇鍖栫殑--涔熷氨鏄锛岃绫诲瀷蹇呴』鎻愪緵涓�釜鏂规硶鏉ヨ绠楀畠鐨勫搱甯屽�銆備竴涓搱甯屽�鏄�code>Int绫诲瀷鐨勶紝鐩哥瓑鐨勫璞″搱甯屽�蹇呴』鐩稿悓锛屾瘮濡�code>a==b,鍥犳蹇呴』a.hashValue == b.hashValue銆�/p>

Swift 鐨勬墍鏈夊熀鏈被鍨�姣斿String,Int,Double鍜�code>Bool)榛樿閮芥槸鍙搱甯屽寲鐨勶紝鍙互浣滀负闆嗗悎鐨勫�鐨勭被鍨嬫垨鑰呭瓧鍏哥殑閿殑绫诲瀷銆傛病鏈夊叧鑱斿�鐨勬灇涓炬垚鍛樺�(鍦�a href="http://www.swift51.com/swift2.1/chapter2/08_Enumerations.html">鏋氫妇鏈夎杩�榛樿涔熸槸鍙搱甯屽寲鐨勩�

娉ㄦ剰锛�br />浣犲彲浠ヤ娇鐢ㄤ綘鑷畾涔夌殑绫诲瀷浣滀负闆嗗悎鐨勫�鐨勭被鍨嬫垨鑰呮槸瀛楀吀鐨勯敭鐨勭被鍨嬶紝浣嗕綘闇�浣夸綘鐨勮嚜瀹氫箟绫诲瀷绗﹀悎 Swift 鏍囧噯搴撲腑鐨�code>Hashable鍗忚銆傜鍚�code>Hashable鍗忚鐨勭被鍨嬮渶瑕佹彁渚涗竴涓被鍨嬩负Int鐨勫彲璇诲睘鎬�code>hashValue銆傜敱绫诲瀷鐨�code>hashValue灞炴�杩斿洖鐨勫�涓嶉渶瑕佸湪鍚屼竴绋嬪簭鐨勪笉鍚屾墽琛屽懆鏈熸垨鑰呬笉鍚岀▼搴忎箣闂翠繚鎸佺浉鍚屻� 

鍥犱负Hashable鍗忚绗﹀悎Equatable鍗忚锛屾墍浠ョ鍚堣鍗忚鐨勭被鍨嬩篃蹇呴』鎻愪緵涓�釜"鏄惁鐩哥瓑"杩愮畻绗�==)鐨勫疄鐜般�杩欎釜Equatable鍗忚瑕佹眰浠讳綍绗﹀悎==瀹炵幇鐨勫疄渚嬮棿閮芥槸涓�鐩哥瓑鐨勫叧绯汇�涔熷氨鏄锛屽浜�code>a,b,c涓変釜鍊兼潵璇达紝==鐨勫疄鐜板繀椤绘弧瓒充笅闈笁绉嶆儏鍐碉細

  • a == a(鑷弽鎬�
  • a == b鎰忓懗鐫�code>b == a(瀵圭О鎬�
  • a == b && b == c鎰忓懗鐫�code>a == c(浼犻�鎬�

鍏充簬绗﹀悎鍗忚鐨勬洿澶氫俊鎭紝璇风湅鍗忚銆�/p>

闆嗗悎绫诲瀷璇硶

Swift 涓殑Set绫诲瀷琚啓涓�code>Set<Element>锛岃繖閲岀殑Element琛ㄧずSet涓厑璁稿瓨鍌ㄧ殑绫诲瀷锛屽拰鏁扮粍涓嶅悓鐨勬槸锛岄泦鍚堟病鏈夌瓑浠风殑绠�寲褰㈠紡銆�/p>

var letters = Set<Character>() print("letters is of type Set<Character> with \(letters.count) items.") // 鎵撳嵃 "letters is of type Set<Character> with 0 items."

娉ㄦ剰锛�br />閫氳繃鏋勯�鍣紝杩欓噷鐨�code>letters鍙橀噺鐨勭被鍨嬭鎺ㄦ柇涓�code>Set<Character>銆�/p>

姝ゅ锛屽鏋滀笂涓嬫枃鎻愪緵浜嗙被鍨嬩俊鎭紝姣斿浣滀负鍑芥暟鐨勫弬鏁版垨鑰呭凡鐭ョ被鍨嬬殑鍙橀噺鎴栧父閲忥紝鎴戜滑鍙互閫氳繃涓�釜绌虹殑鏁扮粍瀛楅潰閲忓垱寤轰竴涓┖鐨�code>Set锛�/p>

letters.insert("a") // letters 鐜板湪鍚湁1涓�Character 绫诲瀷鐨勫� letters = [] // letters 鐜板湪鏄竴涓┖鐨�Set, 浣嗘槸瀹冧緷鐒舵槸 Set<Character> 绫诲瀷 

鐢ㄦ暟缁勫瓧闈㈤噺鍒涘缓闆嗗悎

浣犲彲浠ヤ娇鐢ㄦ暟缁勫瓧闈㈤噺鏉ユ瀯閫犻泦鍚堬紝骞朵笖鍙互浣跨敤绠�寲褰㈠紡鍐欎竴涓垨鑰呭涓�浣滀负闆嗗悎鍏冪礌銆�/p>

涓嬮潰鐨勪緥瀛愬垱寤轰竴涓О涔嬩负favoriteGenres鐨勯泦鍚堟潵瀛樺偍String绫诲瀷鐨勫�锛�/p>

var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"] // favoriteGenres 琚瀯閫犳垚鍚湁涓変釜鍒濆鍊肩殑闆嗗悎 

杩欎釜favoriteGenres鍙橀噺琚0鏄庝负“涓�釜String鍊肩殑闆嗗悎”锛屽啓涓�code>Set<String>銆傜敱浜庤繖涓壒瀹氱殑闆嗗悎鍚湁鎸囧畾String绫诲瀷鐨勫�锛屾墍浠ュ畠鍙厑璁稿瓨鍌�code>String绫诲瀷鍊笺�杩欓噷鐨�code>favoriteGenres鍙橀噺鏈変笁涓�code>String绫诲瀷鐨勫垵濮嬪�("Rock"锛�code>"Classical"鍜�code>"Hip hop")锛屽苟浠ユ暟缁勫瓧闈㈤噺鐨勬柟寮忓嚭鐜般�

娉ㄦ剰锛�br />favoriteGenres琚0鏄庝负涓�釜鍙橀噺(鎷ユ湁var鏍囩ず绗�鑰屼笉鏄竴涓父閲�鎷ユ湁let鏍囩ず绗�,鍥犱负瀹冮噷闈㈢殑鍏冪礌灏嗕細鍦ㄤ笅闈㈢殑渚嬪瓙涓澧炲姞鎴栬�绉婚櫎銆�/p>

涓�釜Set绫诲瀷涓嶈兘浠庢暟缁勫瓧闈㈤噺涓鍗曠嫭鎺ㄦ柇鍑烘潵锛屽洜姝�code>Set绫诲瀷蹇呴』鏄惧紡澹版槑銆傜劧鑰岋紝鐢变簬 Swift 鐨勭被鍨嬫帹鏂姛鑳斤紝濡傛灉浣犳兂浣跨敤涓�釜鏁扮粍瀛楅潰閲忔瀯閫犱竴涓�code>Set骞朵笖璇ユ暟缁勫瓧闈㈤噺涓殑鎵�湁鍏冪礌绫诲瀷鐩稿悓锛岄偅涔堜綘鏃犻』鍐欏嚭Set鐨勫叿浣撶被鍨嬨�favoriteGenres鐨勬瀯閫犲舰寮忓彲浠ラ噰鐢ㄧ畝鍖栫殑鏂瑰紡浠f浛锛�/p>

var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"] 

鐢变簬鏁扮粍瀛楅潰閲忎腑鐨勬墍鏈夊厓绱犵被鍨嬬浉鍚岋紝Swift 鍙互鎺ㄦ柇鍑�code>Set<String>浣滀负favoriteGenres鍙橀噺鐨勬纭被鍨嬨�

print("I have \(favoriteGenres.count) favorite music genres.") // 鎵撳嵃 "I have 3 favorite music genres."

浣跨敤甯冨皵灞炴�isEmpty浣滀负涓�釜缂╁啓褰㈠紡鍘绘鏌�code>count灞炴�鏄惁涓�code>0锛�/p>

if favoriteGenres.isEmpty { print("As far as music goes, I'm not picky.") } else { print("I have particular music preferences.") } // 鎵撳嵃 "I have particular music preferences." 

浣犲彲浠ラ�杩囪皟鐢�code>Set鐨�code>insert(_:)鏂规硶鏉ユ坊鍔犱竴涓柊鍏冪礌锛�/p>

favoriteGenres.insert("Jazz") // favoriteGenres 鐜板湪鍖呭惈4涓厓绱� 

浣犲彲浠ラ�杩囪皟鐢�code>Set鐨�code>remove(_:)鏂规硶鍘诲垹闄や竴涓厓绱狅紝濡傛灉璇ュ�鏄Set鐨勪竴涓厓绱犲垯鍒犻櫎璇ュ厓绱犲苟涓旇繑鍥炶鍒犻櫎鐨勫厓绱犲�锛屽惁鍒欏鏋滆Set涓嶅寘鍚鍊硷紝鍒欒繑鍥�code>nil銆傚彟澶栵紝Set涓殑鎵�湁鍏冪礌鍙互閫氳繃瀹冪殑removeAll()鏂规硶鍒犻櫎銆�/p>

if let removedGenre = favoriteGenres.remove("Rock") { print("\(removedGenre)? I'm over it.") } else { print("I never much cared for that.") } // 鎵撳嵃 "Rock? I'm over it." 

浣跨敤contains(_:)鏂规硶鍘绘鏌�code>Set涓槸鍚﹀寘鍚竴涓壒瀹氱殑鍊硷細

if favoriteGenres.contains("Funk") { print("I get up on the good foot.") } else { print("It's too funky in here.") } // 鎵撳嵃 "It's too funky in here." 

閬嶅巻涓�釜闆嗗悎

浣犲彲浠ュ湪涓�釜for-in寰幆涓亶鍘嗕竴涓�code>Set涓殑鎵�湁鍊笺�

for genre in favoriteGenres { print("\(genre)") } // Classical // Jazz // Hip hop 

鏇村鍏充簬for-in寰幆鐨勪俊鎭紝鍙傝For 寰幆銆�/p>

Swift 鐨�code>Set绫诲瀷娌℃湁纭畾鐨勯『搴忥紝涓轰簡鎸夌収鐗瑰畾椤哄簭鏉ラ亶鍘嗕竴涓�code>Set涓殑鍊煎彲浠ヤ娇鐢�code>sort()鏂规硶锛屽畠灏嗘牴鎹彁渚涚殑搴忓垪杩斿洖涓�釜鏈夊簭闆嗗悎.

for genre in favoriteGenres.sort() { print("\(genre)") } // prints "Classical" // prints "Hip hop" // prints "Jazz 

闆嗗悎鎿嶄綔

浣犲彲浠ラ珮鏁堝湴瀹屾垚Set鐨勪竴浜涘熀鏈搷浣滐紝姣斿鎶婁袱涓泦鍚堢粍鍚堝埌涓�捣锛屽垽鏂袱涓泦鍚堝叡鏈夊厓绱狅紝鎴栬�鍒ゆ柇涓や釜闆嗗悎鏄惁鍏ㄥ寘鍚紝閮ㄥ垎鍖呭惈鎴栬�涓嶇浉浜ゃ�

鍩烘湰闆嗗悎鎿嶄綔

涓嬮潰鐨勬彃鍥炬弿杩颁簡涓や釜闆嗗悎-a鍜�code>b-浠ュ強閫氳繃闃村奖閮ㄥ垎鐨勫尯鍩熸樉绀洪泦鍚堝悇绉嶆搷浣滅殑缁撴灉銆�/p>

  • 浣跨敤intersect(_:)鏂规硶鏍规嵁涓や釜闆嗗悎涓兘鍖呭惈鐨勫�鍒涘缓鐨勪竴涓柊鐨勯泦鍚堛�
  • 浣跨敤exclusiveOr(_:)鏂规硶鏍规嵁鍦ㄤ竴涓泦鍚堜腑浣嗕笉鍦ㄤ袱涓泦鍚堜腑鐨勫�鍒涘缓涓�釜鏂扮殑闆嗗悎銆�/li>
  • 浣跨敤union(_:)鏂规硶鏍规嵁涓や釜闆嗗悎鐨勫�鍒涘缓涓�釜鏂扮殑闆嗗悎銆�/li>
  • 浣跨敤subtract(_:)鏂规硶鏍规嵁涓嶅湪璇ラ泦鍚堜腑鐨勫�鍒涘缓涓�釜鏂扮殑闆嗗悎銆�/li>
let oddDigits: Set = [1, 3, 5, 7, 9] let evenDigits: Set = [0, 2, 4, 6, 8] let singleDigitPrimeNumbers: Set = [2, 3, 5, 7] oddDigits.union(evenDigits).sort() // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] oddDigits.intersect(evenDigits).sort() // [] oddDigits.subtract(singleDigitPrimeNumbers).sort() // [1, 9] oddDigits.exclusiveOr(singleDigitPrimeNumbers).sort() // [1, 2, 9] 

  • 浣跨敤“鏄惁鐩哥瓑”杩愮畻绗�==)鏉ュ垽鏂袱涓泦鍚堟槸鍚﹀寘鍚叏閮ㄧ浉鍚岀殑鍊笺�
  • 浣跨敤isSubsetOf(_:)鏂规硶鏉ュ垽鏂竴涓泦鍚堜腑鐨勫�鏄惁涔熻鍖呭惈鍦ㄥ彟澶栦竴涓泦鍚堜腑銆�/li>
  • 浣跨敤isSupersetOf(_:)鏂规硶鏉ュ垽鏂竴涓泦鍚堜腑鍖呭惈鍙︿竴涓泦鍚堜腑鎵�湁鐨勫�銆�/li>
  • 浣跨敤isStrictSubsetOf(_:)鎴栬�isStrictSupersetOf(_:)鏂规硶鏉ュ垽鏂竴涓泦鍚堟槸鍚︽槸鍙﹀涓�釜闆嗗悎鐨勫瓙闆嗗悎鎴栬�鐖堕泦鍚堝苟涓斾袱涓泦鍚堝苟涓嶇浉绛夈�
  • 浣跨敤isDisjointWith(_:)鏂规硶鏉ュ垽鏂袱涓泦鍚堟槸鍚︿笉鍚湁鐩稿悓鐨勫�銆�/li>
let houseAnimals: Set = ["馃惗", "馃惐"] let farmAnimals: Set = ["馃惍", "馃悢", "馃悜", "馃惗", "馃惐"] let cityAnimals: Set = ["馃惁", "馃惌"] houseAnimals.isSubsetOf(farmAnimals) // true farmAnimals.isSupersetOf(houseAnimals) // true farmAnimals.isDisjointWith(cityAnimals) // true 

瀛楀吀

瀛楀吀鏄竴绉嶅瓨鍌ㄥ涓浉鍚岀被鍨嬬殑鍊肩殑瀹瑰櫒銆傛瘡涓�锛坴alue锛夐兘鍏宠仈鍞竴鐨勯敭锛坘ey锛夛紝閿綔涓哄瓧鍏镐腑鐨勮繖涓�鏁版嵁鐨勬爣璇嗙銆傚拰鏁扮粍涓殑鏁版嵁椤逛笉鍚岋紝瀛楀吀涓殑鏁版嵁椤瑰苟娌℃湁鍏蜂綋椤哄簭銆傛垜浠湪闇�閫氳繃鏍囪瘑绗︼紙閿級璁块棶鏁版嵁鐨勬椂鍊欎娇鐢ㄥ瓧鍏革紝杩欑鏂规硶寰堝ぇ绋嬪害涓婂拰鎴戜滑鍦ㄧ幇瀹炰笘鐣屼腑浣跨敤瀛楀吀鏌ュ瓧涔夌殑鏂规硶涓�牱銆�/p>

娉ㄦ剰锛�br />Swift 鐨�code>Dictionary绫诲瀷琚ˉ鎺ュ埌Foundation鐨�code>NSDictionary绫汇�
鏇村鍏充簬鍦�code>Foundation鍜�code>Cocoa涓娇鐢�code>Dictionary绫诲瀷鐨勪俊鎭紝鍙傝 Using Swift with Cocoa and Objective-C (Swift 2.1) 涓�功銆�/p>

瀛楀吀绫诲瀷蹇嵎璇硶

Swift 鐨勫瓧鍏镐娇鐢�code>Dictionary<Key, Value>瀹氫箟锛屽叾涓�code>Key鏄瓧鍏镐腑閿殑鏁版嵁绫诲瀷锛�code>Value鏄瓧鍏镐腑瀵瑰簲浜庤繖浜涢敭鎵�瓨鍌ㄥ�鐨勬暟鎹被鍨嬨�

娉ㄦ剰锛�br />涓�釜瀛楀吀鐨�code>Key绫诲瀷蹇呴』閬靛惊Hashable鍗忚锛屽氨鍍�code>Set鐨勫�绫诲瀷銆�/p>

鎴戜滑涔熷彲浠ョ敤[Key: Value]杩欐牱蹇嵎鐨勫舰寮忓幓鍒涘缓涓�釜瀛楀吀绫诲瀷銆傝櫧鐒惰繖涓ょ褰㈠紡鍔熻兘涓婄浉鍚岋紝浣嗘槸鍚庤�鏄閫夛紝骞朵笖杩欐湰鎸囧涔︽秹鍙婂埌瀛楀吀绫诲瀷鏃堕�绡囬噰鐢ㄥ悗鑰呫�

var namesOfIntegers = [Int: String]() // namesOfIntegers 鏄竴涓┖鐨�[Int: String] 瀛楀吀

杩欎釜渚嬪瓙鍒涘缓浜嗕竴涓�code>[Int: String]绫诲瀷鐨勭┖瀛楀吀鏉ュ偍瀛樻暣鏁扮殑鑻辫鍛藉悕銆傚畠鐨勯敭鏄�code>Int鍨嬶紝鍊兼槸String鍨嬨�

濡傛灉涓婁笅鏂囧凡缁忔彁渚涗簡绫诲瀷淇℃伅锛屾垜浠彲浠ヤ娇鐢ㄧ┖瀛楀吀瀛楅潰閲忔潵鍒涘缓涓�釜绌哄瓧鍏革紝璁颁綔[:]锛堜腑鎷彿涓斁涓�釜鍐掑彿锛夛細

namesOfIntegers[16] = "sixteen" // namesOfIntegers 鐜板湪鍖呭惈涓�釜閿�瀵� namesOfIntegers = [:] // namesOfIntegers 鍙堟垚涓轰簡涓�釜 [Int: String] 绫诲瀷鐨勭┖瀛楀吀 

鐢ㄥ瓧鍏稿瓧闈㈤噺鍒涘缓瀛楀吀

鎴戜滑鍙互浣跨敤瀛楀吀瀛楅潰閲忔潵鏋勯�瀛楀吀锛岃繖鍜屾垜浠垰鎵嶄粙缁嶈繃鐨勬暟缁勫瓧闈㈤噺鎷ユ湁鐩镐技璇硶銆傚瓧鍏稿瓧闈㈤噺鏄竴绉嶅皢涓�釜鎴栧涓敭鍊煎鍐欎綔Dictionary闆嗗悎鐨勫揩鎹烽�寰勩�

涓�釜閿�瀵规槸涓�釜key鍜屼竴涓�code>value鐨勭粨鍚堜綋銆傚湪瀛楀吀瀛楅潰閲忎腑锛屾瘡涓�釜閿�瀵圭殑閿拰鍊奸兘鐢卞啋鍙峰垎鍓层�杩欎簺閿�瀵规瀯鎴愪竴涓垪琛紝鍏朵腑杩欎簺閿�瀵圭敱鏂规嫭鍙峰寘鍚�鐢遍�鍙峰垎鍓诧細

[key 1: value 1, key 2: value 2, key 3: value 3] 

涓嬮潰鐨勪緥瀛愬垱寤轰簡涓�釜瀛樺偍鍥介檯鏈哄満鍚嶇О鐨勫瓧鍏搞�鍦ㄨ繖涓瓧鍏镐腑閿槸涓変釜瀛楁瘝鐨勫浗闄呰埅绌鸿繍杈撶浉鍏充唬鐮侊紝鍊兼槸鏈哄満鍚嶇О锛�/p>

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

airports瀛楀吀琚0鏄庝负涓�[String: String]绫诲瀷锛岃繖鎰忓懗鐫�繖涓瓧鍏哥殑閿拰鍊奸兘鏄�code>String绫诲瀷銆�/p>

娉ㄦ剰锛�br />airports瀛楀吀琚0鏄庝负鍙橀噺锛堢敤var鍏抽敭瀛楋級鑰屼笉鏄父閲忥紙let鍏抽敭瀛楋級鍥犱负鍚庢潵鏇村鐨勬満鍦轰俊鎭細琚坊鍔犲埌杩欎釜绀轰緥瀛楀吀涓�

airports瀛楀吀浣跨敤瀛楀吀瀛楅潰閲忓垵濮嬪寲锛屽寘鍚袱涓敭鍊煎銆傜涓�鐨勯敭鏄�code>YYZ锛屽�鏄�code>Toronto Pearson銆傜浜屽鐨勯敭鏄�code>DUB锛屽�鏄�code>Dublin銆�/p>

杩欎釜瀛楀吀璇彞鍖呭惈浜嗕袱涓�code>String: String绫诲瀷鐨勯敭鍊煎銆傚畠浠搴�code>airports鍙橀噺澹版槑鐨勭被鍨嬶紙涓�釜鍙湁String閿拰String鍊肩殑瀛楀吀锛夋墍浠ヨ繖涓瓧鍏稿瓧闈㈤噺鐨勪换鍔℃槸鏋勯�鎷ユ湁涓や釜鍒濆鏁版嵁椤圭殑airport瀛楀吀銆�/p>

鍜屾暟缁勪竴鏍凤紝鎴戜滑鍦ㄧ敤瀛楀吀瀛楅潰閲忔瀯閫犲瓧鍏告椂锛屽鏋滃畠鐨勯敭鍜屽�閮芥湁鍚勮嚜涓�嚧鐨勭被鍨嬶紝閭d箞灏变笉蹇呭啓鍑哄瓧鍏哥殑绫诲瀷銆�nbsp;airports瀛楀吀涔熷彲浠ョ敤杩欑绠�煭鏂瑰紡瀹氫箟锛�/p>

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

鍥犱负杩欎釜璇彞涓墍鏈夌殑閿拰鍊奸兘鍚勮嚜鎷ユ湁鐩稿悓鐨勬暟鎹被鍨嬶紝Swift 鍙互鎺ㄦ柇鍑�code>Dictionary<String, String>鏄�code>airports瀛楀吀鐨勬纭被鍨嬨�

print("The dictionary of airports contains \(airports.count) items.") // 鎵撳嵃 "The dictionary of airports contains 2 items."锛堣繖涓瓧鍏告湁涓や釜鏁版嵁椤癸級

浣跨敤甯冨皵灞炴�isEmpty鏉ュ揩鎹峰湴妫�煡瀛楀吀鐨�code>count灞炴�鏄惁绛変簬0锛�/p>

if airports.isEmpty { print("The airports dictionary is empty.") } else { print("The airports dictionary is not empty.") } // 鎵撳嵃 "The airports dictionary is not empty." 

鎴戜滑涔熷彲浠ュ湪瀛楀吀涓娇鐢ㄤ笅鏍囪娉曟潵娣诲姞鏂扮殑鏁版嵁椤广�鍙互浣跨敤涓�釜鎭板綋绫诲瀷鐨勯敭浣滀负涓嬫爣绱㈠紩锛屽苟涓斿垎閰嶆伆褰撶被鍨嬬殑鏂板�锛�/p>

airports["LHR"] = "London" // airports 瀛楀吀鐜板湪鏈変笁涓暟鎹」 

鎴戜滑涔熷彲浠ヤ娇鐢ㄤ笅鏍囪娉曟潵鏀瑰彉鐗瑰畾閿搴旂殑鍊硷細

airports["LHR"] = "London Heathrow" // "LHR"瀵瑰簲鐨勫� 琚敼涓�"London Heathrow 

浣滀负鍙︿竴绉嶄笅鏍囨柟娉曪紝瀛楀吀鐨�code>updateValue(_:forKey:)鏂规硶鍙互璁剧疆鎴栬�鏇存柊鐗瑰畾閿搴旂殑鍊笺�灏卞儚涓婇潰鎵�ず鐨勪笅鏍囩ず渚嬶紝updateValue(_:forKey:)鏂规硶鍦ㄨ繖涓敭涓嶅瓨鍦ㄥ搴斿�鐨勬椂鍊欎細璁剧疆鏂板�鎴栬�鍦ㄥ瓨鍦ㄦ椂鏇存柊宸插瓨鍦ㄧ殑鍊笺�鍜屼笂闈㈢殑涓嬫爣鏂规硶涓嶅悓鐨勶紝updateValue(_:forKey:)杩欎釜鏂规硶杩斿洖鏇存柊鍊间箣鍓嶇殑鍘熷�銆傝繖鏍蜂娇寰楁垜浠彲浠ユ鏌ユ洿鏂版槸鍚︽垚鍔熴�

updateValue(_:forKey:)鏂规硶浼氳繑鍥炲搴斿�鐨勭被鍨嬬殑鍙�鍊笺�涓句緥鏉ヨ锛氬浜庡瓨鍌�code>String鍊肩殑瀛楀吀锛岃繖涓嚱鏁颁細杩斿洖涓�釜String?鎴栬�“鍙� String”绫诲瀷鐨勫�銆�/p>

濡傛灉鏈夊�瀛樺湪浜庢洿鏂板墠锛屽垯杩欎釜鍙�鍊煎寘鍚簡鏃у�锛屽惁鍒欏畠灏嗕細鏄�code>nil銆�/p>

if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") { print("The old value for DUB was \(oldValue).") } // 杈撳嚭 "The old value for DUB was Dublin." 

鎴戜滑涔熷彲浠ヤ娇鐢ㄤ笅鏍囪娉曟潵鍦ㄥ瓧鍏镐腑妫�储鐗瑰畾閿搴旂殑鍊笺�鍥犱负鏈夊彲鑳借姹傜殑閿病鏈夊搴旂殑鍊煎瓨鍦紝瀛楀吀鐨勪笅鏍囪闂細杩斿洖瀵瑰簲鍊肩殑绫诲瀷鐨勫彲閫夊�銆傚鏋滆繖涓瓧鍏稿寘鍚姹傞敭鎵�搴旂殑鍊硷紝涓嬫爣浼氳繑鍥炰竴涓寘鍚繖涓瓨鍦ㄥ�鐨勫彲閫夊�锛屽惁鍒欏皢杩斿洖nil锛�/p>

if let airportName = airports["DUB"] { print("The name of the airport is \(airportName).") } else { print("That airport is not in the airports dictionary.") } // 鎵撳嵃 "The name of the airport is Dublin Airport." 

鎴戜滑杩樺彲浠ヤ娇鐢ㄤ笅鏍囪娉曟潵閫氳繃缁欐煇涓敭鐨勫搴斿�璧嬪�涓�code>nil鏉ヤ粠瀛楀吀閲岀Щ闄や竴涓敭鍊煎锛�/p>

airports["APL"] = "Apple Internation" // "Apple Internation" 涓嶆槸鐪熺殑 APL 鏈哄満, 鍒犻櫎瀹� airports["APL"] = nil // APL 鐜板湪琚Щ闄や簡 

姝ゅ锛�code>removeValueForKey(_:)鏂规硶涔熷彲浠ョ敤鏉ュ湪瀛楀吀涓Щ闄ら敭鍊煎銆傝繖涓柟娉曞湪閿�瀵瑰瓨鍦ㄧ殑鎯呭喌涓嬩細绉婚櫎璇ラ敭鍊煎骞朵笖杩斿洖琚Щ闄ょ殑鍊兼垨鑰呭湪娌℃湁鍊肩殑鎯呭喌涓嬭繑鍥�code>nil锛�/p>

if let removedValue = airports.removeValueForKey("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." 

瀛楀吀閬嶅巻

鎴戜滑鍙互浣跨敤for-in寰幆鏉ラ亶鍘嗘煇涓瓧鍏镐腑鐨勯敭鍊煎銆傛瘡涓�釜瀛楀吀涓殑鏁版嵁椤归兘浠�code>(key, value)鍏冪粍褰㈠紡杩斿洖锛屽苟涓旀垜浠彲浠ヤ娇鐢ㄤ复鏃跺父閲忔垨鑰呭彉閲忔潵鍒嗚В杩欎簺鍏冪粍锛�/p>

for (airportCode, airportName) in airports { print("\(airportCode): \(airportName)") } // YYZ: Toronto Pearson // LHR: London Heathrow 

鏇村鍏充簬for-in寰幆鐨勪俊鎭紝鍙傝For 寰幆銆�/p>

閫氳繃璁块棶keys鎴栬�values灞炴�锛屾垜浠篃鍙互閬嶅巻瀛楀吀鐨勯敭鎴栬�鍊硷細

for airportCode in airports.keys { print("Airport code: \(airportCode)") } // Airport code: YYZ // Airport code: LHR for airportName in airports.values { print("Airport name: \(airportName)") } // Airport name: Toronto Pearson // Airport name: London Heathrow 

濡傛灉鎴戜滑鍙槸闇�浣跨敤鏌愪釜瀛楀吀鐨勯敭闆嗗悎鎴栬�鍊奸泦鍚堟潵浣滀负鏌愪釜鎺ュ彈Array瀹炰緥鐨�API 鐨勫弬鏁帮紝鍙互鐩存帴浣跨敤keys鎴栬�values灞炴�鏋勯�涓�釜鏂版暟缁勶細

let airportCodes = [String](airports.keys) // airportCodes 鏄�["YYZ", "LHR"] let airportNames = [String](airports.values) // airportNames 鏄�["Toronto Pearson", "London Heathrow"] 

Swift 鐨勫瓧鍏哥被鍨嬫槸鏃犲簭闆嗗悎绫诲瀷銆備负浜嗕互鐗瑰畾鐨勯『搴忛亶鍘嗗瓧鍏哥殑閿垨鍊硷紝鍙互瀵瑰瓧鍏哥殑keys鎴�code>values灞炴�浣跨敤sort()鏂规硶銆�/p>

你可能感兴趣的:(Swift-2.4闆嗗悎绫诲瀷)