TIMEX TIMEX - 1 year ago 104
iOS Question

In Swift, how do I set every element inside an array to nil?

var roomsLiveStates = [Firebase?]()

for ref in roomsLiveStates {
if ref != nil {
ref = nil

This doesn't seem to work.

Rob Rob
Answer Source

You can just set each to nil:

for index in 0 ..< roomsLiveStates.count {
    roomsLiveStates[index] = nil

As The Swift Programming Language says in its Control Flow discussion of for syntax:

This example prints the first few entries in the five-times-table:

for index in 1...5 {
    println("\(index) times 5 is \(index * 5)")

... In the example above, index is a constant whose value is automatically set at the start of each iteration of the loop. As such, it does not have to be declared before it is used. It is implicitly declared simply by its inclusion in the loop declaration, without the need for a let declaration keyword.

As this says, the index is a constant. As such, you can not change its value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download