2016-07-15 55 views
1

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

回答

-1

我想我發現我一直在尋找的答案。我將在我將要使用的代碼應用於所使用的示例下面發佈。感謝您的回答。

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

1

您使用與關聯值的枚舉的方式如下:

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

我剛剛發現了一個更好的答案,我在這裏發佈。非常感謝您的時間。 –

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

我剛剛在這裏發現了一個更好的答案。非常感謝您的時間。 –

2

枚舉與關聯值不能是字符串類型。但是他們可以實現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