Friday, November 6, 2026
HomeiOS Developmentios - Swift: Struct Array vs Class Array

ios – Swift: Struct Array vs Class Array

[ad_1]

Mutating a struct saved inside another property behaves as if you have copied out the worth, modified it, and overwrote it again into place.

Take this line for instance: (I changed the optionally available chaining with pressure unwrapping, for simplicity)

array.first!.title = "dddddd"

It behaves as if you probably did:

var tmp = array.first!
tmp.title = "dddddd"
array.first = tmp

It is easy to see what that does not work. Array.first, is a get-only property (it would not have a setter).

The case for classses works as a result of the worth saved within the array is a reference to the item, and the reference is not altering (solely the values inside the object it refers to, which the array would not know or care about).

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments