1

My code is not working. All I am trying to do is get the number inserted into the struct's internal array. Right now this is not working:

@IBAction func move(_ sender: Any) {
    bad.numbers.insert(0, at: 0)
}


struct bad {
    var numbers: [Int] = [1, 2, 3]
}

1 Answer 1

3

You need to declare your numbers property as static. Btw it is Swift convention to name your structures starting with an uppercase letter:

struct Bad {
    static var numbers: [Int] = [1, 2, 3]
}

And to insert elements at index 0, you need to call it like this :

Bad.numbers.insert(0, at: 0)

print(Bad.numbers)  // "[0, 1, 2, 3]\n"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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