gpt4 book ai didi

swift - 如何在 swift 中声明类型别名和枚举?

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:21 24 4
gpt4 key购买 nike

我从 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);

到目前为止 swift

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)”)

最佳答案

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

关于swift - 如何在 swift 中声明类型别名和枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24261851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com