0

I have this code for a question and answer app i want to make. It is supposed to ask a random question from the array. My code is working in playground, but when i put it into an xcode project, it tells me:

Cannot use instance member 'Kat1' within property initializer; property initializers run before 'self' is available

Im new to coding apps and swift, and i dont know how to init the array the right way.

Here is the code im using:

var Kat1: [(question: String, answer: String)] =
    [
        ("What is the capital of Alabama?", "Montgomery"),
        ("What is the capital of Alaska?", "Juneau"),
        ("What is the capital of Test?", "Test Town")
]

var antal = (Kat1.count)
var randomtal = Int(arc4random_uniform(UInt32(antal)))

print(Kat1[randomtal].question)
print(Kat1[randomtal].answer)`

What am i doing wrong?

5
  • I believe you'll need to put it inside a function or viewDidLoad() Commented Oct 5, 2016 at 14:40
  • Remember that arc4Random is not truly random, you might end up with the same number over and over again. Commented Oct 5, 2016 at 14:43
  • @Joakim No, "rand" is pseudo random, but "arc4random_uniform" is random. Commented Oct 5, 2016 at 14:47
  • Morten will get random numbers, but he can end up with the same question over and over again. for index in 1...3{ let random = Int(arc4random_uniform(UInt32(Int(3)))) print(random) } //result is: 0 2 0 Commented Oct 5, 2016 at 14:58
  • I will have a lot more questions, so i guess the chance for getting same question two times will be less. Commented Oct 5, 2016 at 15:32

2 Answers 2

2

Put your code inside a function....

   override func viewDidLoad() {
    super.viewDidLoad()

var Kat1: [(question: String, answer: String)] =
[
    ("What is the capital of Alabama?", "Montgomery"),
    ("What is the capital of Alaska?", "Juneau"),
    ("What is the capital of Test?", "Test Town")
]

var antal = (Kat1.count)
var randomtal = Int(arc4random_uniform(UInt32(antal)))

print(Kat1[randomtal].question)
print(Kat1[randomtal].answer)`
}
Sign up to request clarification or add additional context in comments.

2 Comments

Nice... This works.. Can you tell me why it needs to be there?
0

You'll probably want to package the questions and answers into a structure that can hold them all and deliver individual questions upon request. That will allow you to keep the code that stores the questions, and the code that presents the questions, separate.

struct Quiz {
    var kat1: [(question: String, answer: String)] =
        [("What is the capital of Alabama?", "Montgomery"),
         ("What is the capital of Alaska?", "Juneau"),
         ("What is the capital of Test?", "Test Town")]

    func randomQuestion() -> (question: String, answer: String) {
        let randomtal = Int(arc4random_uniform(UInt32(kat1.count)))
        return kat1[randomtal]
    }
}

let quiz = Quiz()
let qa   = quiz.randomQuestion()
print(qa.question)
print(qa.answer)

2 Comments

This still gives the same error. Does this need to be inside viewDidLoad() also? Cannot use instance member 'quiz' within property initializer; property initializers run before 'self' is available And how do i put the qa.question and qa.answer on a label or bottom in the storyboard.. If i put it inside viewDidLoad() i just get the same error when i ry to use it outside viewDidLoad() somewhere
The struct can stand on its own, in a separate file. The final four lines go in the viewDidLoad(). The final two print statements should then be replaced to fit the rest of your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.