【问题标题】:Swift enum with associated values带有关联值的 Swift 枚举
【发布时间】:2016-07-15 09:37:38
【问题描述】:

到目前为止,我一直在使用 rawValue 根据本例中的情况获取文本:

enum Level1: String {
    case question1 = "q1"
    case question2 = "q2"
    case question3 = "q3"
}

print(Level1.question1.rawValue)

但现在我也想知道答案。我试图让它与关联的值一起工作,但我既不知道如何将值分配给属性,也不知道它是否可能。例如:questionText = "q1", answerText = "a1"。

enum Level2 {
    case question1(questionText: String, answerText: String);
    case question2(questionText: String, answerText: String);
    case question3(questionText: String, answerText: String);
}

【问题讨论】:

    标签: swift enums


    【解决方案1】:

    具有关联值的枚举不能是字符串类型。但他们可以实施例如CustomStringConvertible 协议:

    enum ScreenName: CustomStringConvertible {
    
        case Category(categoryId: String, categoryName: String)
        ...
    
        var description: String {
            switch (self) {
            case let .Category(_, categoryName):
                return "Category - \(categoryName)"
            ...
            }
        }
    }
    

    那么你可以打电话给ScreenName.Category(categoryId: "1", categoryName: "products").description,你会得到"Category - products"

    【讨论】:

    • 我需要分别访问这两个项目以访问已选择的枚举案例。例如:ScreenName.categoryName 和 ScreenName.categoryId
    • 我刚刚找到了一个更好的答案,我在这里发布了。非常感谢您的宝贵时间。
    • 您可以拥有任意数量的计算变量(例如,对于名称和 ID)...
    【解决方案2】:

    您可以通过以下方式使用具有关联值的枚举:

    enum Level2 {
        case question1(questionText: String, answerText: String)
        case question2(questionText: String, answerText: String)
        case question3(questionText: String, answerText: String)
    }
    
    // initialise
    let level = Level2.question2(questionText: "Question", answerText: "Answer")
    
    // read
    
    switch level {
    case .question1(let question, let answer):
        print(question + answer)
    case .question2(let question, let answer):
        print("something")
    case .question3(let question, let answer):
        print("something else")
    }
    

    我建议在这里阅读更多内容:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html

    P.S:如果您真的需要在您的案例中使用枚举而不是结构,我建议您重新考虑一下。 使用结构的一个例子是:

    struct Level {
        let question: String
        let answer: String
    }
    
    let levels = [Level]()
    

    但这是另一个话题。我希望我已经回答了你的问题。

    【讨论】:

    • 我知道您想向我展示什么,但是您如何为每个案例存储不同的“问题”和“答案”?在那里你只分配给第二个
    • 我刚刚找到了一个更好的答案,我在这里发布了。非常感谢您的宝贵时间。
    【解决方案3】:

    关联值与RawRepresentable(你的第一个枚举)不同,它的值是在运行时分配的,并且每次使用都可能不同。基本上,具有关联值的枚举不适合您的需求。

    试试这个:

    struct Challenge {
    
        let question: String
        let answer: String?
    }
    
    struct Level1 {
    
        static let challenge1 = Challenge(question: "q1", answer: nil)
        static let challenge2 = Challenge(question: "q2", answer: nil)
        static let challenge3 = Challenge(question: "q3", answer: nil)
    }
    
    struct Level2 {
    
        static let challenge1 = Challenge(question: "q1", answer: "a1")
        static let challenge2 = Challenge(question: "q2", answer: "a3")
        static let challenge3 = Challenge(question: "q3", answer: "a3")
    }
    

    稍后您可以检索它们:

    let currentChallenge = Level1.challenge2
    

    【讨论】:

    • 我理解并喜欢您的方法,但我需要使用枚举的东西,因为我一直在检查枚举状态。我知道在这种情况下使用它会更容易,但问题/答案只是代表情况而不是真实代码的示例。
    • 实现Equatable,然后你可以检查它与你的状态是否相等。
    • 我刚刚找到了一个更好的答案,我在这里发布了。非常感谢您的宝贵时间。
    【解决方案4】:

    我想我找到了我正在寻找的答案。我将在下面发布我将要使用的代码应用于所使用的示例。感谢您的回答。

        enum Level1: String {
        case question1 = "q1";
        case question2 = "q2";
        case question3 = "q3";
    
        init() {
            self = Level1.question1
        }
    
        func answer() -> String {
            switch self {
            case .question1:
                return "a1"
            case .question2:
                return "a2"
            case . question3:
                return "a3"
            }
        }
    }
    
    var currentLevel: Level1 = Level1();
    print(currentLevel.rawValue);  //returns "q1"
    print(currentLevel.answer());  //returns "a1"
    

    【讨论】:

    • 但您必须为每个级别编写相同的结构。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多