user8105388
user8105388

Reputation:

How to insert number in struct array (Swift4)

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]
}

Upvotes: 1

Views: 90

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236420

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"

Upvotes: 3

Related Questions