Swift4 基礎(chǔ)部分: Enumerations

本文是學(xué)習(xí)《The Swift Programming Language》整理的相關(guān)隨筆华坦,基本的語(yǔ)法不作介紹,主要介紹Swift中的一些特性或者與OC差異點(diǎn)金踪。

系列文章:

Enumerations

You introduce enumerations with the enum keyword and place 
their entire definition within a pair of braces:

enum SomeEnumeration {
    // enumeration definition goes here
}

例子:

enum CompassPoint {
    case north
    case south
    case east
    case west
} 
Unlike C and Objective-C, Swift enumeration cases are not 
assigned a default integer value when they are created.
  • Swift中枚舉不會(huì)存在默認(rèn)值

關(guān)聯(lián)值(Associated Values)

You can define Swift enumerations to store associated 
values of any given type, and the value types can be 
different for each case of the enumeration if needed. 
Enumerations similar to these are known as discriminated 
unions, tagged unions, or variants in other programming 
languages.
  • Swift中的枚舉可以將每個(gè)枚舉值關(guān)聯(lián)上指定的一些值浊洞,這些值類型也可以不同

例子:

enum Shape{
    case Rectangle(x:Float,y:Float,width:Float,height:Float)
    case Circle((x:Float,y:Float),radius:Float)
}

func judgeShape(_ shape:Shape){
    switch shape {
    case Shape.Rectangle(let x,let y,let width,let height):
        print("Rectangle -> x:\(x),y:\(y),width:\(width),height:\(height)");
    case Shape.Circle((let x,let y), let radius):
        print("Circle -> x:\(x),y:\(y),radius:\(radius)");
    }
}

var rectangle = Shape.Rectangle(x: 10, y: 10, width: 100, height: 50);
var circle = Shape.Circle((x: 10, y: 10), radius: 100);
judgeShape(rectangle);
judgeShape(circle);

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

Rectangle -> x:10.0,y:10.0,width:100.0,height:50.0
Circle -> x:10.0,y:10.0,radius:100.0

原始數(shù)值(Raw Values)

Raw values can be strings, characters, or any of the 
integer or floating-point number types. Each raw value 
must be unique within its enumeration declaration.
  • Swift中的枚舉的值可以是字符串,字符胡岔,整型等多種數(shù)據(jù)類型

隱式賦值(Implicitly Assigned Raw Values)

For instance, when integers are used for raw values, the 
implicit value for each case is one more than the previous 
case. If the first case doesn’t have a value set, its 
value is 0.

When strings are used for raw values, the implicit value 
for each case is the text of that case’s name.
  • Swift中枚舉如果指定了數(shù)據(jù)類型法希,默認(rèn)整型就是0開(kāi)始,字符串就是枚舉的名字

例子:

enum CompassPoint: String{
    case north, south, east, west
}
print(CompassPoint.north.rawValue);

enum Planet: Int{
    case mercury = 0, venus, earth, mars, jupiter, saturn, uranus, neptune
}
print(Planet.mercury.rawValue);

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

north
0

遞歸枚舉(Recursive Enumerations)

A recursive enumeration is an enumeration that has another 
instance of the enumeration as the associated value for 
one or more of the enumeration cases. You indicate that an 
enumeration case is recursive by writing indirect before 
it, which tells the compiler to insert the necessary layer 
of indirection.
  • Swift中存在遞歸枚舉的寫法,需要使用關(guān)鍵字indirect修飾它

例子:

indirect enum ArithmeticExpression {
    case number(Int)
    case addition(ArithmeticExpression, ArithmeticExpression)
    case multiplication(ArithmeticExpression, ArithmeticExpression)
}

let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))

func evaluate(_ expression: ArithmeticExpression) -> Int {
    switch expression {
    case let .number(value):
        return value
    case let .addition(left, right):
        return evaluate(left) + evaluate(right)
    case let .multiplication(left, right):
        return evaluate(left) * evaluate(right)
    }
}

print(evaluate(product))

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

18

個(gè)人覺(jué)得上述方式放入枚舉中寫很奇怪靶瘸,覺(jué)得應(yīng)該函數(shù)的方式去實(shí)現(xiàn)才更容易理解铁材,這里只是說(shuō)明了一種用法

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市奕锌,隨后出現(xiàn)的幾起案子著觉,更是在濱河造成了極大的恐慌落君,老刑警劉巖萍肆,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宙拉,死亡現(xiàn)場(chǎng)離奇詭異泛鸟,居然都是意外死亡嗓违,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門社证,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)警医,“玉大人,你說(shuō)我怎么就攤上這事典徘◇翱粒” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵逮诲,是天一觀的道長(zhǎng)帜平。 經(jīng)常有香客問(wèn)我,道長(zhǎng)梅鹦,這世上最難降的妖魔是什么裆甩? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮齐唆,結(jié)果婚禮上嗤栓,老公的妹妹穿的比我還像新娘。我一直安慰自己箍邮,他們只是感情好茉帅,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著锭弊,像睡著了一般担敌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上廷蓉,一...
    開(kāi)封第一講書(shū)人閱讀 51,679評(píng)論 1 305
  • 那天全封,我揣著相機(jī)與錄音,去河邊找鬼桃犬。 笑死刹悴,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的攒暇。 我是一名探鬼主播土匀,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼形用!你這毒婦竟也來(lái)了就轧?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤田度,失蹤者是張志新(化名)和其女友劉穎妒御,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體镇饺,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡乎莉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惋啃。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡哼鬓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出边灭,到底是詐尸還是另有隱情异希,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布绒瘦,位于F島的核電站称簿,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏椭坚。R本人自食惡果不足惜予跌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一搏色、第九天 我趴在偏房一處隱蔽的房頂上張望善茎。 院中可真熱鬧,春花似錦频轿、人聲如沸垂涯。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)耕赘。三九已至,卻和暖如春膳殷,著一層夾襖步出監(jiān)牢的瞬間操骡,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工赚窃, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留册招,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓勒极,卻偏偏與公主長(zhǎng)得像是掰,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子辱匿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

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

  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young閱讀 3,814評(píng)論 1 10
  • 枚舉為一組相關(guān)的值定義了一個(gè)共同的類型键痛,使得可以在代碼中以類型安全的方式來(lái)使用這些值。 如果熟悉 C 語(yǔ)言匾七,會(huì)知道...
    窮人家的孩紙閱讀 854評(píng)論 1 4
  • 如果是通過(guò)官方的EaseUI來(lái)進(jìn)行獲取會(huì)話列表.在官方文檔上面可以看到,通過(guò)初始化EaseConversation...
    郭杭閱讀 1,889評(píng)論 2 4
  • 歲月磨平身上的棱角絮短,變的圓滑,暮色的風(fēng)景昨忆,漸漸灰暗戚丸,不放棄的人生,相約暮年 從前,開(kāi)朗的自己限府,變的不愛(ài)言語(yǔ)夺颤,或許這...
    靈殤雪閱讀 523評(píng)論 0 2
  • Clipboard引薦 Clipboard,即剪切板胁勺,當(dāng)我們同時(shí)按動(dòng)Ctrl+C時(shí)世澜,選定的對(duì)象就被存放在了剪切板中...
    BarretX閱讀 5,253評(píng)論 0 2