1

Sample code:

struct test {
    var itest: Int?;
}

var tests : [test] = [];

var t = test();

tests.append(t)

t.itest = 99;

print(tests[0].itest)
print(t.itest)

produces

nil
Optional(99)

So it seems the append command creates a copy of t and not its reference. If the array would contain a reference of t, both print outs must be itest=99.

frameworker
  • 298
  • 1
  • 3
  • 16
  • 2
    Does this answer your question? [Is Swift Pass By Value or Pass By Reference](https://stackoverflow.com/questions/27364117/is-swift-pass-by-value-or-pass-by-reference) – SamB Sep 14 '21 at 18:30
  • In Swift only classes are reference types. structs are value types. – EmilioPelaez Sep 14 '21 at 18:30

1 Answers1

1

A struct is not an "object" in the way you are probably thinking. It's a value. When you assign a value, it's copied. That's by design.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610