如何在swift中声明typealias和enum?

时间:2014-06-17 10:48:28

标签: swift enums typedef

我从Lynda那里得到了这个例子,我试图将它转换为Swift但是有错误。有人会有任何想法如何成功地将其转换为Swift?

目标-C

typedef enum { window = 99, aisle = 199, middle = 399 } seatPreference;

int main(int argc, const char * argv[]) {
@autoreleasepool {

    seatPreference bobSeatPreference = aisle;
    seatPreference fredSeatPreference = window;


    if (bobSeatPreference == window) {
        // do something
    }

    NSLog(@"Fred wants %i", fredSeatPreference);
到目前为止,斯威夫特

typealias enum { case Window = 99, case Aisle = 199, case Middle = 399 } = seatPreference

import Foundation

seatPreference bobSeatPreference = aisle
seatPreference fredSeatPreference = window

if (bobSeatPreference == window) {
    // do something
}

printLn(“Fred wants \(fredSeatPreference)”)

1 个答案:

答案 0 :(得分:0)

typedef enum变成了Swift enum。 您的代码可以按如下方式重写:

enum SeatPreference:Int {
    case Window = 99
    case Aisle = 199
    case Middle = 399
}

var bobSeatPreference:SeatPreference = .Aisle
var fredSeatPreference:SeatPreference = .Window

if (bobSeatPreference == .Window) {
    // do something
}

SeatPreference.Window.toRaw() //this prints 99