Swift 3.0 學(xué)習(xí)筆記之Collection (Array , Set , Dictionary)

Array 數(shù)組 ?相當(dāng)于OC中的可變數(shù)組

var someInts = [Int]() //初始化? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? someInts.append(3) // 添加一個元素? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? someInts= [] // 制空數(shù)組?

var threeDoubles = Array(repeating:0.0,count:3) // 初始化數(shù)組? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var anotherThreeDoubles = Array(repeating:2.5,count:3) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var sixDoubles = threeDoubles + anotherThreeDoubles // 合并兩個數(shù)組 ( 相當(dāng)于 第一個數(shù)組 依次添加第二個數(shù)組里面的元素)

var shoppingList: [String] = ["Eggs","Milk"] //用字面量形式 初始化數(shù)組? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var shoppingList = ["Eggs","Milk"] ? ? ? shoppingList[4...6] = ["Bananas","Apples"]//改變一定范圍內(nèi)的元素? ? shoppingList.insert("Maple Syrup",at:0)//插入元素 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? let mapleSyrup = shoppingList.remove(at:0) //移除元素

for (index,value) in shoppingList.enumerated() {//獲取數(shù)組元素以及對應(yīng)的索引

? ? ? ?print("Item\(index+1):\(value)")

}

Sets 集合

var letters = Set<Character>() //初始化集合? letters.insert("a")//插入元素 letters = [] // 制空集合? var favoriteGenres:Set<String> = ["Rock","Classical","Hip hop"] // 字面量初始化? ? ? ? ? ? ? var favoriteGenres:Set= ["Rock","Classical","Hip hop"] // 自行推斷是字符串類型的集合? ? ? ? ? favoriteGenres.isEmpty// 判空 favoriteGenres.insert("Jazz") // 插入元素 favoriteGenres.remove("Rock") // 刪除元素 favoriteGenres.contains("Funk")//包含

for genre in favoriteGenres.sorted() { //按照索引排序 和數(shù)組一樣

? ? ? print("\(genre)")

}

a.intersection(b)// 求a和b的交集? a.symmetricDifference(b)// a和b的非交集 a.union(b) // a和b的并集 a.subtracting(b) // a中除了b的元素剩下的

let houseAnimals:Set= ["??","??"]

let farmAnimals:Set= ["??","??","??","??","??"]

let cityAnimals:Set= ["??","??"]

houseAnimals.isSubset(of:farmAnimals) //是否是子集 ? ?// true

farmAnimals.isSuperset(of:houseAnimals) //是否包含 ? ?// true

farmAnimals.isDisjoint(with:cityAnimals) // 是否有交集 ? ??// true

Dictionary 字典

var namesOfIntegers = [Int:String]() //字典初始化 // namesOfIntegers is an empty [Int: String] dictionary? [ key : value ]? ? namesOfIntegers[16] ="sixteen" // namesOfIntegers now contains 1 key-value pair? ? namesOfIntegers= [:] // namesOfIntegers is once again an empty dictionary of type [Int: String]? var airports: [String:String] = ["YYZ":"Toronto Pearson","DUB":"Dublin"] //字典初始化 var airports= ["YYZ":"Toronto Pearson","DUB":"Dublin"] //和上句一樣 自行推斷類型 ?

if airports.isEmpty { // 判空

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

}else{

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

} ? ?// Prints "The airports dictionary is not empty."

airports["LHR"] ="London" // 訪問值

if let oldValue = airports.updateValue("Dublin Airport",forKey:"DUB") { // 更換對應(yīng)key 的 value

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

} ? ?// Prints "The old value for DUB was Dublin."

if let removedValue = airports.removeValue(forKey:"DUB") { // 移除對應(yīng)key的value

? ? ? ?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 (airportCode,airportName) in ?airports{ // 遍歷字典

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

} ? ??// YYZ: Toronto Pearson ? ? ??// LHR: London Heathrow

for airportCode in airports.keys { // 遍歷字典的所有key

? ? print("Airport code:\(airportCode)")

} ? ?// Airport code: YYZ ? ? ? ?// Airport code: LHR

for airportName in airports.values{ // 遍歷字典所有的value

? ? ?print("Airport name:\(airportName)")

} ? ??// Airport name: Toronto Pearson ? ? ? ?// Airport name: London Heathrow

let airportCodes = [String](airports.keys) // ?獲取字典的所有的key 返回一個數(shù)組

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

let airportNames = [String](airports.values) // 獲取字典所有的value 返回一個數(shù)組

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末个唧,一起剝皮案震驚了整個濱河市岛心,隨后出現(xiàn)的幾起案子盼砍,更是在濱河造成了極大的恐慌妆丘,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件技竟,死亡現(xiàn)場離奇詭異冰肴,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進(jìn)店門嚼沿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來估盘,“玉大人,你說我怎么就攤上這事骡尽∏餐祝” “怎么了?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵攀细,是天一觀的道長箫踩。 經(jīng)常有香客問我,道長谭贪,這世上最難降的妖魔是什么境钟? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮俭识,結(jié)果婚禮上慨削,老公的妹妹穿的比我還像新娘。我一直安慰自己套媚,他們只是感情好缚态,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著堤瘤,像睡著了一般玫芦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上本辐,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天桥帆,我揣著相機(jī)與錄音,去河邊找鬼慎皱。 笑死老虫,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的茫多。 我是一名探鬼主播祈匙,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼地梨!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起缔恳,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤宝剖,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后歉甚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體万细,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年墅诡,在試婚紗的時候發(fā)現(xiàn)自己被綠了带猴。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片睬澡。...
    茶點(diǎn)故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡制跟,死狀恐怖凡橱,靈堂內(nèi)的尸體忽然破棺而出蔚晨,到底是詐尸還是另有隱情兵扬,我是刑警寧澤酸休,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布献起,位于F島的核電站洋访,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏谴餐。R本人自食惡果不足惜姻政,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望岂嗓。 院中可真熱鬧汁展,春花似錦、人聲如沸厌殉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽年枕。三九已至炫欺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間熏兄,已是汗流浹背品洛。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留摩桶,地道東北人桥状。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像硝清,于是被迫代替她去往敵國和親辅斟。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 1 .數(shù)組 Arrays 數(shù)組使用有序列表存儲同一類型的多個值芦拿。相同的值可以多次出現(xiàn)在一個數(shù)組的不同位置中士飒。這和O...
    iceMaple閱讀 465評論 0 1
  • 53.計算字符 在字符串中獲取字符值的數(shù)量, 可以使用字符串字符屬性中的計數(shù)屬性: let unusualMena...
    無灃閱讀 1,091評論 0 4
  • //var intArray:Array = [Int]();varintArray = [Int]();prin...
    XLsn0w閱讀 436評論 0 0
  • 是誰酵幕,給了她走向茫茫大漠的勇氣,是誰缓苛,給了她舍己救國的正義芳撒,是誰,給了她毅然決然百折不撓的毅力?你看,她得到來吹落...
    生活戀情閱讀 241評論 0 0
  • 昨天好友說自己被朋友刪了微信笔刹,qq芥备,想想就很氣憤,有什么事不能說開舌菜,非要鬧的跟絕交一樣萌壳。問起原因,僅僅是因?yàn)楹糜褵o...
    小懶說Yolo閱讀 195評論 0 0