鏈〉鍖呭惈鍐呭锛�/p>
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>
鏁扮粍浣跨敤鏈夊簭鍒楄〃瀛樺偍鍚屼竴绫诲瀷鐨勫涓�銆傜浉鍚岀殑鍊煎彲浠ュ娆″嚭鐜板湪涓�釜鏁扮粍鐨勪笉鍚屼綅缃腑銆�/p>
娉ㄦ剰: Swift 鐨�code>Array绫诲瀷琚ˉ鎺ュ埌
Foundation
涓殑NSArray
绫汇� 鏇村鍏充簬鍦�code>Foundation鍜�code>Cocoa涓娇鐢�code>Array鐨勪俊鎭紝鍙傝 Using Swift with Cocoa and Obejective-C 涓�功銆�/p>
娉ㄦ剰锛岄�杩囨瀯閫犲嚱鏁扮殑绫诲瀷锛�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>
浣跨敤甯冨皵鍊煎睘鎬�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" 瀛楃涓�
濡傛灉鎴戜滑鍚屾椂闇�姣忎釜鏁版嵁椤圭殑鍊煎拰绱㈠紩鍊硷紝鍙互浣跨敤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>
涓�釜绫诲瀷涓轰簡瀛樺偍鍦ㄩ泦鍚堜腑锛岃绫诲瀷蹇呴』鏄彲鍝堝笇鍖栫殑--涔熷氨鏄锛岃绫诲瀷蹇呴』鎻愪緵涓�釜鏂规硶鏉ヨ绠楀畠鐨勫搱甯屽�銆備竴涓搱甯屽�鏄�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>
娉ㄦ剰锛�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
鍙橀噺鐨勬纭被鍨嬨�
浣跨敤甯冨皵灞炴�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]
杩欐牱蹇嵎鐨勫舰寮忓幓鍒涘缓涓�釜瀛楀吀绫诲瀷銆傝櫧鐒惰繖涓ょ褰㈠紡鍔熻兘涓婄浉鍚岋紝浣嗘槸鍚庤�鏄閫夛紝骞朵笖杩欐湰鎸囧涔︽秹鍙婂埌瀛楀吀绫诲瀷鏃堕�绡囬噰鐢ㄥ悗鑰呫�
杩欎釜渚嬪瓙鍒涘缓浜嗕竴涓�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瀛楀吀鐨勬纭被鍨嬨�
浣跨敤甯冨皵灞炴�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>