Swift: For-In Loop Requires '[Deepspeechtokenmetadata]' to Conform to 'Sequence'

Swift: For-in loop requires '[DeepSpeechTokenMetadata]' to conform to 'Sequence'

You can either do this, where x is the index and token is the element:

for (x, token) in transcriptCandidate.enumerated() {
}

Or this if you don't need the index:

for token in transcriptCandidate {
}

For-in loop requires '[String]?' to conform to 'Sequence'; did you mean to unwrap optional? but I don't think I am using optionals

Swift dictionary accesses always return optional values, because the lookup will return nil if the key doesn't exist. It is up to you to handle this resulting optional is a safe way. Adding ! is rarely the right way to fix it because your code will crash if nil is returned.

The first time you get an optional is here:

for arr in adictionary[key] {

Again, Swift doesn't know if key exists in adictionary so it returns an optional. A safe way to fix this is to use the dictionary lookup which returns a default value when the key doesn't exist. In this case, returning an empty array seems like a good choice since your for loop will then just do nothing:

for arr in adictionary[key, default:[]] {

Next, you get an optional here:

if !(adictionary[arr].contains(key)){

Again, you need to decide how to safely handle the fact that dictionary[arr] could return nil. The same trick works here: return an empty array if arr doesn't exist:

if !(adictionary[arr, default:[]].contains(key)){

Here is the final version of your code:

var adictionary = [String:[String]]()
adictionary["A"] = ["B", "C"]
adictionary["B"] = ["A", "C"]
adictionary["C"] = ["A"]
adictionary["D"] = []

var newdic = Array(adictionary.keys).reduce(into: [String: Bool]()) { $0[$1] = false } //I make an arr from a dictionary's keys of type [String:[String]]
print(newdic)

for key in newdic.keys {
for arr in adictionary[key, default:[]] {
if !(adictionary[arr, default:[]].contains(key)){
newdic[key] = true
}
}
}

Only continue within for loop if button was pressed

Here is some pseudo code

// create an array to hold your answers
var answers: [Answer] = []()
// create an array to hold your questions
var questions: [Question]
var currentQuestion: Question {
didSet {
// every time this property updates show the new question
self.showQuestion(self.currentQuestion)
}
}

init(questions: [Question]) {
// store the questions
self.questions = questions
// pop your first questions from the list and assign it as the current question
// this would also trigger its didSet method to display the question
self.currentQuestion = self.questions.pop()
}

func showQuestion(_ question: Question) {
// display question on screen
questionLabel.text = question.text
firstButton.title = question.firstOption
secondButton.title = question.secondOption
}

func onButtonClick(sender: UIButton) {
// user selected an answer, you can now store it and move to your next question. Here is how you can do it

// create your answer and store it
let answer = Answer(currentQuestion.text, answer: sender.title)
self.answers.append(answer)
// check if there is more questions in the list
if self.questions.count > 0 {
there is more questions so take one and show it the user
self.currentQuestion = self.questions.pop()
} else {
// you have finished with all questions
// you have successfully looped through all questions
// and all user answers are now stored in your answers array
// do with your answers whatever you need to. You're now done with asking questions :)
}
}

This demonstrates the logic you could use to achieve what you're looking for. There is no need to implement for loops as you only need to wait for the user input. Once you receive the user input you store it and move to the next question and this is how you will eventually loop through all the questions. Hope it makes sense



Related Topics



Leave a reply



Submit