Reputation: 1956
I do not know exactly what I am looking for, that is the reason why I am opening a new question. If there is another question which is answered, please mark this question as a duplicate.
Please understand I am new to IOS development, so if I write something that is not quite right, feel free to correct me.
So I have an array in my swift code with six indexes. Every index has a value of four different variables. For example in my QuestionBank
class:
var list = [Question]()
init() {
list.append(Question(text: "some Text", answerA: "answer a"), answerB: "answer b", selectedQuestionNumber: 1)
list.append(Question(text: "some Text", answerA: "answer a"), answerB: "answer b", selectedQuestionNumber: 2)
list.append(Question(text: "some Text", answerA: "answer a"), answerB: "answer b", selectedQuestionNumber: 3)
list.append(Question(text: "some Text", answerA: "answer a"), answerB: "answer b", selectedQuestionNumber: 4)
list.append(Question(text: "some Text", answerA: "answer a"), answerB: "answer b", selectedQuestionNumber: 5)
list.append(Question(text: "some Text", answerA: "answer a"), answerB: "answer b", selectedQuestionNumber: 6)
}
I added the selectedQuestionNumber
to have a unique identifier for each index. I do not want to use the default number of every index (allQuestions.list[questionNumber].questionText
)
So my question is the following:
How can I get the values of the first index using the selectedQuestionNumber
as the identifier?
So far i got this:
if allQuestions.list[questionNumber].storyNumber == 1 {
storyTextView.text = allQuestions.list[questionNumber].questionText
topButton.setTitle(allQuestions.list[questionNumber].answerA, for: .normal)
bottomButton.setTitle(allQuestions.list[questionNumber].answerB, for: .normal)
}
I evaluate if the storyNumber is 1 but the text and the answers won't be evaluated by the same factor. I would like to have everything evaluated by the storyNumber. That means that the [questionNumber]
must be replaced with something else and give me the text and the answers that belong to storyNumber 1.
If you need more information about the code and the variables that are not defined (at least here, on my project they are defined), feel free to ask.
Best regards
Upvotes: 1
Views: 163
Reputation: 1915
using first(where: {$0.selectionNumber == VALUE})
is indeed what you are looking for if you just want to head straight forward.
Since you want to index the array yourself, i would suggest doing it this way tho. I rewrote your Question struct to represent my design:
public struct Question {
let text: String
let answerA: String
let answerB: String
init(_ text: String, _ answerA: String, _ answerB: String) {
self.text = text
self.answerA = answerA
self.answerB = answerB
}
}
class Test {
var list: [Int : Question] = [:]
init() {
list[0] = Question("some text", "answer A", "Answer B")
list[1] = Question("some text", "answer A", "Answer B")
list[2] = Question("some text", "answer A", "Answer B")
list[3] = Question("some text", "answer A", "Answer B")
list[4] = Question("some text", "answer A", "Answer B")
list[5] = Question("some text", "answer A", "Answer B")
}
}
This way you will use an dictionary
that you can index yourself and fetch the same way as an array.
This way you don't have to zero index it either, but you can start at 15 and go up, or you can use negative values if you so wish.
Upvotes: 0
Reputation: 1707
You’re looking to use first(where: )
. It’s done like this:
let question = list.first(where: { $0.selectedQuestionNumber == 1 })
You can also sort the array by selectedQuestionNumber
like this:
let orderedList = list.sorted { $0.selectedQuestionNumber < $1.selectedQuestionNumber }
Or, the mutating version:
list.sort { $0.selectedQuestionNumber < $1.selectedQuestionNumber }
To which you can then access them by their array index.
And on top of all of that, there is filter:
let question = list.filter { $0.selectedQuestionNumber == 3 }
But be aware, since you’re using it in this context, that the result will still be an array. There may be none, one, or multiple values that remain.
Upvotes: 1