swift4 收藏類型

  • swift 提供了array,set, dictionary 等三個(gè)收藏類型。
  • 創(chuàng)建一個(gè)空數(shù)組 array:就是數(shù)組
var someInts = [Int]()
print("數(shù)組里有\(zhòng)(someInts.count)個(gè)元素")
//會(huì)打印數(shù)組里有0個(gè)元素
someInts.append(7)
//現(xiàn)在someInts包含了一個(gè)值
someInts = []
//現(xiàn)在它是一個(gè)空數(shù)組了,他的類型也是依然int
創(chuàng)建有默認(rèn)值得數(shù)組

swift 有一個(gè)數(shù)組初始化器,他能創(chuàng)建特定大小的數(shù)組

var threeDoubles = Array(repeatElement(7.7, count: 3))
//兩個(gè)特定的數(shù)組還可以相加
var anotherThreeDoubles = Array(repeatElement(3.3, count: 3))
var sixDoubles = threeDoubles + anotherThreeDoubles
字符串?dāng)?shù)組
var shoppingList: [String] = ["Eggs","Milik"]
var list = ["book" ,"12"]

//下面代碼可以簡(jiǎn)寫數(shù)組
var shoppingList1 = ["apple","orange","banana"]
訪問和修改數(shù)組
//count 能查找數(shù)組中的項(xiàng)目數(shù)
print("shoppinglist 數(shù)組有\(zhòng)(shoppingList.count)項(xiàng)目")
//數(shù)組是不是等于零的檢查方式
if shoppingList.isEmpty{
    print("no one")
}else{
    print("數(shù)組不是空的")
}
//如果我想添加一個(gè)新成員的話會(huì)調(diào)用append(_:)注意他會(huì)添加到最后一個(gè)位置
shoppingList.append("phone")
//我還可以  += 來添加新成員
shoppingList += ["beer"]
索引來修改數(shù)組的值
shoppingList[0] = "hony"
//如果想特定的位置加成員,我想位置1 和 2的成員改掉
shoppingList[1...2] = ["butter","beef"]

//我要插進(jìn)去一個(gè)成員的話呢用這個(gè)方法

shoppingList.insert("coffe", at: 3)
shoppingList.insert("tea", at: 3)

//相反remove(at:)方法來刪掉特定位置的成員
shoppingList.remove(at: 3)
shoppingList.removeLast()
shoppingList.count

//for item in array 是遍歷整個(gè)數(shù)組的
for nl_item in shoppingList{
    print(nl_item)
}
set 集合
//set 存儲(chǔ)沒有特定循序的,相同類型的不同的值
//創(chuàng)建和初始化一個(gè)空集
var letters = Set<Character>()
print("\(letters.count)")
//會(huì)打印零


letters.insert("m")
letters = []
//現(xiàn)在集又空了 ,屬性依然是Set<Character>
創(chuàng)建一個(gè)字符串的集合
//var favoriteGenres: Set<String> = ["rock","hiphop","classical"]
//可以簡(jiǎn)寫
var favoriteGenres: Set = ["rock","hiphop","classical"]

let oddDigits: Set = [1,3,5,7,9]
let evenDigits: Set = [0,2,4,6,8]
let singleDigitNumber: Set = [2,3,5,7]

oddDigits.union(evenDigits).sorted()
//最后的sorted()方法將排序集合元祖如果把它刪除不會(huì)報(bào)錯(cuò)就是沒有循序

//intersection 中文意思:交叉;十字路口;交集;交叉點(diǎn) 它就是交集!
oddDigits.intersection(evenDigits).sorted()

// subtracting:差集
oddDigits.subtracting(evenDigits).sorted()
字典
  • 創(chuàng)建一 個(gè)空字典
//其實(shí)他也與數(shù)組一樣
var namesOfinteger = [Int: String]()
//上面一行代碼可以這麼理解:我要頂一個(gè)字典,他key的類型是int,value的類型是string
namesOfinteger[27] = "is my age"
//下面這一行代碼是空字典
namesOfinteger = [:]
  • 創(chuàng)建一個(gè)字符串字典
//[key1:value1, key2: value2, key3: value3]
//var airports: [String: String] = ["0998" : "喀什","0991":"烏魯木齊","0995":"吐魯番"]
//上面代碼中數(shù)組是地區(qū)號(hào)
//簡(jiǎn)寫的話可以這么些
var airports = ["0998" : "喀什","0991":"烏魯木齊","0995":"吐魯番"]
訪問和修改字典
  • 數(shù)組一樣用count 來查找其中的item數(shù)
print("the air ports dictionary contains \(airports.count) item")

//同樣適用isEmptyE來檢查是否空
if airports.isEmpty{
    print("the airport dictionary is empty.")
}else{
    print("the airport dictionary is not empty .")
}

//你拿字典的key 修改它的值
airports["0998"] = "喀什人民歡迎你"

if let oldValue = airports.updateValue("烏魯木齊人民歡迎你", forKey: "0991"){
    print("the old value for 烏魯木齊 was\(oldValue)")
}
//if let airportName = airports["0991"]{
//    print("the name of the airport is \(airportName)")
//}
  • 你可以遍歷字典的每一個(gè)key和value
for (airportCode,airportName) in airports{
    print("\(airportCode):\(airportName)")
    
}

for airportCode in airports.keys{
    print("airportCode: \(airportCode)")
}

for airportName in airports.values{
    print("airportName:\(airportName)")
}
初始化
let airportCode1 = [String](airports.keys)
let airportName1 = [String](airports.values)
print(airportCode1)
print(airportName1)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 由蘋果官網(wǎng)翻譯得來fork自https://github.com/letsswift/The-Swift-Prog...
    佛祖拿屠刀閱讀 436評(píng)論 0 1
  • 53.計(jì)算字符 在字符串中獲取字符值的數(shù)量, 可以使用字符串字符屬性中的計(jì)數(shù)屬性: let unusualMena...
    無灃閱讀 1,128評(píng)論 0 4
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,188評(píng)論 4 61
  • 面臨畢業(yè),最后的我們,也是最無助的我們。 都說在大學(xué)要談一場(chǎng)戀愛,因?yàn)檫M(jìn)入社會(huì)你會(huì)發(fā)現(xiàn)再也沒有上學(xué)時(shí)談的戀愛純真,...
    獨(dú)獨(dú)獨(dú)角獸閱讀 195評(píng)論 0 0
  • 美國(guó)野外紀(jì)錄片《荒野求生》當(dāng)中的主角-貝爺,一個(gè)號(hào)稱生活在食物鏈最頂端的男人,靠著掌握的眾多野外生存知識(shí)和技...
    番薯都督閱讀 282評(píng)論 0 2