我有一个包含50多个问题的iOS应用。每个问题都有:
最初我尝试使用结构(如下所示)并为每个问题创建一个常量,当用户得到该问题时我可以调用/加载:
struct QuestionInfo {
var question: String
var correctAnswer: String
var wrongAnswer_1: String?
var wrongAnswer_2: String?
var wrongAnswer_3: String?
var imageName: String
}
然而,我被告知这是一个愚蠢的想法,我应该使用数组。我理解数组是针对这种类型的东西,但我如何保持它有条理?
好像我有六个阵列:
let question = ["Is Mars a planet?", "Is Spain in Africa?", "+50 more"]
let correctAnswer = ["Yes", "No", "+50 more"]
//+Four more arrays
每个阵列的长度不超过50个,每个问题将同时存在于六个不同的列表中,这使我很难保持井井有条。我应该为每个问题创建一个数组/结构,然后创建一个数组/结构数组吗?
let questionPackage = QuestionInfo(
question: "Is Mars a planet?",
correctAnswer: "Yes",
wrongAnswer_1: "No",
wrongAnswer_2: "No one knows.",
wrongAnswer_3: "It's a candy bar."
imageName: "MarsPhoto",
)
let questionPackage2 = QuestionInfo(
question: "Is Spain in Africa?",
correctAnswer: "No",
wrongAnswer_1: "Yes",
wrongAnswer_2: "It's up for debate",
wrongAnswer_3: "Duh, it's in South America."
imageName: "SpanishFlag",
)
//Array of structs
let array = [questionPackage, questionPackage2, etc]
我正在尝试根据最有效的运行方式和正常的设计模式进行选择。
答案 0 :(得分:4)
实际上我没有看到像你这样的结构有任何问题。也许只是略有改进
struct Question {
let question: String
let correctAnswer: String
let wrongAnswers: (String, String, String)
let imageName: String
}
let question = Question(
question: "First captain of the Enterprise?",
correctAnswer: "Pike",
wrongAnswers: ("Kirk", "Picard", "Archer"),
imageName: "Enterprise"
)
print(question.question)
print(question.correctAnswer)
print(question.wrongAnswers.0)
print(question.wrongAnswers.1)
print(question.wrongAnswers.2)
答案 1 :(得分:1)
使用如下所示的结构数组
STRUCT
struct Question {
var q: String
var a: String
var wa: (String, String, String)
}
阵列
let array = [Question(q: "q", a: "a", wa: ("wa", "wa", "wa")), Question(q: "q", a: "a", wa: ("wa", "wa", "wa"))]
用法
print(array[0].q)
print(array[0].a)
print(array[0].wa)