SWIFT: Using reference in array instead of copying Object

196 views Asked by At

I use an array of objects. Then I add some of them to another array. But they are copied. So when I change their content the original value remains unchanged.

var primary = [fileObj]()

for image in images {
    if image.oldID != 0 && image.priority != noPriority {
        primary.append(image)
    }
}

How can I add a reference in the array, so I'm able to change the content directly in the object?

E.g.:

primary[2].priority = -1
1

There are 1 answers

0
giorashc On

From the given code the only reason this should happen is if fileObj is a struct. struct instances are copied by value while class instances are passed by reference so change fileObj to be a class rather than a struct.