sdasdadas sdasdadas - 16 days ago 6
Swift Question

What is the scope of a local variable appended to a global array?

In the following example, what is the scope of

localVariable
?

Is the object in
anArray[0] === localVariable
? Will
localVariable
persist until
Example
goes out of scope (from wherever it is constructed)?

class Example {
var anArray: [DataClass] = []

init() {
let localVariable = DataClass()
anArray.append(localVariable)
}
}

class DataClass { }

Answer

Adding it to the array will increment the retain count, so yes, it will exist until the array is destroyed (when the example instance is removed) unless it is added to some other variable/array/set/reference somewhere else as well.