gpt4 book ai didi

ios - 我如何组织这些数据?

转载 作者:搜寻专家 更新时间:2023-11-01 06:05:42 26 4
gpt4 key购买 nike

我有一个包含 50 多个问题的 iOS 应用。每个问题都有:

  1. 一个问题
  2. 正确答案
  3. 三个错误答案
  4. 图像名称

最初我尝试使用一个结构(如下所示)并为每个问题创建一个常量,当用户遇到该问题时我可以调用/加载该常量:

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]

我正在尝试根据最有效运行的方式和正常的设计模式来进行选择。

最佳答案

实际上,我没有发现像您这样的结构有任何问题。也许只是略有改善

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)

关于ios - 我如何组织这些数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38297481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com