Swift4 基礎(chǔ)部分:Collection Types

本文是學(xué)習(xí)《The Swift Programming Language》整理的相關(guān)隨筆幢哨,基本的語法不作介紹奸焙,主要介紹Swift中的一些特性或者與OC差異點津坑。

系列文章:

Generic Collections

Swift’s array, set, and dictionary types are implemented 
as generic collections. For more on generic types and 
collections, see Generics.
  • Swift中集合的實現(xiàn)都使用到泛型的概念黄选。

Mutability of Collections

If you create an array, a set, or a dictionary, and assign 
it to a variable, the collection that is created will be 
mutable. This means that you can change (or mutate) the 
collection after it is created by adding, removing, or changing items in the collection. If you assign an array, 
a set, or a dictionary to a constant, that collection is 
immutable, and its size and contents cannot be changed. 
  • Swift中集合用let修飾的就是不可變集合知纷,var修飾的就是可變集合。

例子:

let threeDoubles = Array(repeatElement(0.0, count: 3));
threeDoubles.append(1.0);

編譯失敿锌住:

MyPlayground.playground:146:1: note: change 'let' to 'var' 
to make it mutable
let threeDoubles = Array(repeatElement(0.0, count: 3));
^~~
var

Arrays

Array Type Shorthand Syntax

The type of a Swift array is written in full as 
Array<Element>, where Element is the type of values the 
array is allowed to store. You can also write the type of 
an array in shorthand form as [Element]. Although the two 
forms are functionally identical, the shorthand form is 
preferred and is used throughout this guide when referring 
to the type of an array.
  • Swift集合數(shù)組的存在兩種方式的寫法被盈,推薦使用[Element]的方式。

例子:

var someInts = [Int]();
print("someInts \(someInts.count)");

var otherSomeInts = Array<Int>();
print("someInts \(otherSomeInts.count)");

Iterating Over an Array

以下兩種方式都可以使用搭伤,特別說明第二種每次遍歷拿到的是一個元組(index,value)只怎。

例子:

var colors = ["red","green","yellow"];
for color in colors{
    print("color:\(color)");
}

for (index,color) in colors.enumerated(){
    print("index:\(index),color:\(color)");
}

執(zhí)行結(jié)果:

color:red
color:green
color:yellow
index:0,color:red
index:1,color:green
index:2,color:yellow

Sets

Hash Values for Set Types

A type must be hashable in order to be stored in a set—
that is, the type must provide a way to compute a hash 
value for itself. A hash value is an Int value that is the 
same for all objects that compare equally, such that if a 
== b, it follows that a.hashValue == b.hashValue.
  • Swift中的Sets設(shè)計與OC中都一樣需要通過hash值來與其他的存儲數(shù)據(jù)進(jìn)行比較。

Dictionaries

Creating a Dictionary with a Dictionary Literal

A key-value pair is a combination of a key and a value. In 
a dictionary literal, the key and value in each key-value 
pair are separated by a colon. The key-value pairs are 
written as a list, separated by commas, surrounded by a 
pair of square brackets:

 [key 1: value 1, key 2: value 2, key 3: value 3]
  • 與OC中字典的賦值存在不同格式怜俐,只是搞不懂通用的{}為什么Swift中不采用身堡,而是使用[](OC,js中都是{}的設(shè)計)佑菩。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市裁赠,隨后出現(xiàn)的幾起案子殿漠,更是在濱河造成了極大的恐慌,老刑警劉巖佩捞,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绞幌,死亡現(xiàn)場離奇詭異,居然都是意外死亡一忱,警方通過查閱死者的電腦和手機莲蜘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進(jìn)店門谭确,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人票渠,你說我怎么就攤上這事逐哈。” “怎么了问顷?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵昂秃,是天一觀的道長。 經(jīng)常有香客問我杜窄,道長肠骆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任塞耕,我火速辦了婚禮蚀腿,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘扫外。我一直安慰自己莉钙,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布畏浆。 她就那樣靜靜地躺著胆胰,像睡著了一般。 火紅的嫁衣襯著肌膚如雪刻获。 梳的紋絲不亂的頭發(fā)上蜀涨,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天,我揣著相機與錄音蝎毡,去河邊找鬼厚柳。 笑死,一個胖子當(dāng)著我的面吹牛沐兵,可吹牛的內(nèi)容都是我干的别垮。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼扎谎,長吁一口氣:“原來是場噩夢啊……” “哼碳想!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起毁靶,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤胧奔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后预吆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體龙填,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了岩遗。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扇商。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖宿礁,靈堂內(nèi)的尸體忽然破棺而出案铺,到底是詐尸還是另有隱情,我是刑警寧澤窘拯,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布红且,位于F島的核電站,受9級特大地震影響涤姊,放射性物質(zhì)發(fā)生泄漏暇番。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一思喊、第九天 我趴在偏房一處隱蔽的房頂上張望壁酬。 院中可真熱鬧,春花似錦恨课、人聲如沸舆乔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽希俩。三九已至,卻和暖如春纲辽,著一層夾襖步出監(jiān)牢的瞬間颜武,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工拖吼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留鳞上,地道東北人。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓吊档,卻偏偏與公主長得像篙议,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子怠硼,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

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