'Strange error in swift which I can't figure out. Code is exactly the same as in textbook. Error in three places
So I have been working on this project for a couple days as there are lots of things to link and code to write. Despite the fact that it is guided using the swift fundamentals book thing, I can't figure out why I am getting the error that I am since everything is written exactly as it is in the book. I am getting an error saying can't convert string to type [Answer]
import UIKit
class QuestionViewController: UIViewController {
@IBOutlet var singleStackView: UIStackView!
@IBOutlet var singleButton1: UIButton!
@IBOutlet var singleButton2: UIButton!
@IBOutlet var singleButton3: UIButton!
@IBOutlet var singleButton4: UIButton!
@IBOutlet var multipleStackView: UIStackView!
@IBOutlet var multiLabel1: UILabel!
@IBOutlet var multiLabel2: UILabel!
@IBOutlet var multiLabel3: UILabel!
@IBOutlet var multiLabel4: UILabel!
@IBOutlet var rangedStackView: UIStackView!
@IBOutlet var rangedLabel1: UILabel!
@IBOutlet var rangedLabel2: UILabel!
@IBOutlet var questionLabel: UILabel!
@IBOutlet var questionProgressView: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
func updateUI() {
singleStackView.isHidden = true
multipleStackView.isHidden = true
rangedStackView.isHidden = true
let currentQuestion = questions[questionIndex]
let currentAnswers = currentQuestion.text
let totalProgress = Float(questionIndex) /
Float(questions.count)
navigationItem.title = "Question #\(questionIndex + 1)"
questionLabel.text = currentQuestion.text
switch currentQuestion.type {
case .single:
updateSingleStack(using: currentAnswers) //error 1
case .multiple:
updateMultipleStack(using: currentAnswers) //error 2
case .ranged:
updateRangedStack(using: currentAnswers) //error 3
}
}
func updateSingleStack(using answers: [Answer]) {
singleStackView.isHidden = false
singleButton1.setTitle(answers[0].text, for: .normal)
singleButton2.setTitle(answers[1].text, for: .normal)
singleButton3.setTitle(answers[2].text, for: .normal)
singleButton4.setTitle(answers[3].text, for: .normal)
}
func updateMultipleStack(using answers: [Answer]) {
multipleStackView.isHidden = false
multiLabel1.text = answers[0].text
multiLabel2.text = answers[1].text
multiLabel3.text = answers[2].text
multiLabel4.text = answers[3].text
}
func updateRangedStack(using answers: [Answer]) {
rangedStackView.isHidden = false
rangedLabel1.text = answers.first?.text
rangedLabel2.text = answers.last?.text
}
var questionIndex = 0
var questions: [Question] = [
Question(
text: "Which food do you like the most?",
type: .single,
answers: [
Answer(text: "Steak", type: .dog),
Answer(text: "Fish", type: .cat),
Answer(text: "Carrots", type: .rabbit),
Answer(text: "Corn", type: .turtle)
]
),
Question(
text: "Which activities do you enjoy?",
type: .multiple,
answers: [
Answer(text: "Swimming", type: .turtle),
Answer(text: "Sleeping", type: .cat),
Answer(text: "Cuddling", type: .rabbit),
Answer(text: "Eating", type: .dog)
]
),
Question(
text: "How much do you enjoy car rides?",
type: .ranged,
answers: [
Answer(text: "I dislike them", type: .cat),
Answer(text: "I get a little nervous", type: .rabbit),
Answer(text: "I barely notice them", type: .turtle),
Answer(text: "I love them", type: .dog)
]
)
]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
