sdasdadas sdasdadas - 9 months ago 44
Swift Question

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

In the following example, what is the scope of


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

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

init() {
let localVariable = DataClass()

class DataClass { }

Answer Source

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.