我沒能到wrongAnswers組合成一個陣列,但我可以通過存儲陣列中的所有的結構使用這種設計模式,這確保他們都使用:
//Question one
let questionPackage = QuestionInfo(
questionNum: 1,
questionType: "Single",
answer: "Correct answer here",
wrongAnswer_1: "This is wrong",
wrongAnswer_2: "So is this",
wrongAnswer_3: "So is not right"
)
//Question two
let questionPackage2 = QuestionInfo(
questionNum: 1,
questionType: "Single",
answer: "Correct answer here",
wrongAnswer_1: "This is wrong",
wrongAnswer_2: "So is this",
wrongAnswer_3: "So is not right"
)
let questionArray = [questionPackage,questionPackage2]
let loadQuestion = questionArray[questionNum]
1)這是一個非常可怕的設計選擇。你需要保留幾十個變量來指向相同的事物。你需要重複一遍又一遍。例如,這就是陣列的用途。 2)這個警告意味着你根本沒有使用你的一個變量。可能的意思是,你的其中一個問題從未被使用。 3)張貼實際的代碼和真實的錯誤信息 - 就目前而言,我們只能猜測。 – Eiko
幾乎總是當你用1,2,3列舉變量時,你正在做一些非常錯誤的事情。使用數組和索引。 – luk2302