gpt4 book ai didi

ios - 通过ID从数组中提取文本

转载 作者:行者123 更新时间:2023-12-01 19:34:18 25 4
gpt4 key购买 nike

我有这个数组,我想按顺序在文本标签中打印问题。是否有一个功能可以读取每个问题的ID并对其进行排序?

这是我的代码:

struct Question {
let question: String
let id: Int
let answers: [ Answer ]
}

struct Answer {
let id: String
let answer: String
let isSelected: Bool
}

struct allQuestions {

let Questions = [

Question(question: "The easiest way to learn is:", id: 1, answers: [
Answer(id: "V", answer: "By viewing, reading, and observing how the others carry out certain tasks", isSelected: false),
Answer(id: "A", answer: "By listening, discussing and doing according to verbal instructions" , isSelected: false),
Answer(id: "K", answer: "By dping and experimenting by myself", isSelected: false)
]),

最佳答案

按ID对问题进行排序

let sorted =  questions.sorted(by: { $0.id < $1.id })

之后,您可以循环数组
for question in sorted 
{
print(question.id)
}

关于ios - 通过ID从数组中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60664600/

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