斯威夫特枚举与关联值

问题描述:

到目前为止,我一直在使用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); 
} 

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

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" 
+0

但您必须为每个级别编写相同的结构。 – Fujia

您使用与关联值的枚举的方式如下:

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

PS:我会建议重新考虑,如果你真的需要在你的情况下使用枚举而不是结构体。 使用结构的一个示例是:

struct Level { 
    let question: String 
    let answer: String 
} 

let levels = [Level]() 

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

+0

我知道你在给我看什么,但是如何为每个案例存储不同的“问题”和“答案”?在那里,你只分配到第二个 –

+0

我刚刚发现了一个更好的答案,我在这里发布。非常感谢您的时间。 –

相关值与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") 
} 

之后你可以retrive他们:

let currentChallenge = Level1.challenge2 
+0

我理解并喜欢你的方法,但我需要一些与枚举工作,因为我全部时间检查枚举状态。我知道在这种情况下使用这个会更容易,但问题/答案仅仅是一个例子来表示情况而不是真实的代码。 –

+0

实现'Equatable',然后你可以检查它与你的状态是否相等。 – Fujia

+0

我刚刚在这里发现了一个更好的答案。非常感谢您的时间。 –

枚举与关联值不能是字符串类型。但是他们可以实现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"

+0

我需要分别访问两个项目到已经选择的枚举情况。像例如:ScreenName.categoryName和ScreenName.categoryId –

+0

我刚刚发现了一个更好的答案,我发布在这里。非常感谢您的时间。 –

+0

您可以根据需要选择尽可能多的计算变量(例如,对于名称和ID)...... – JMI