Swift Language Structs Structs are value types

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Unlike classes, which are passed by reference, structures are passed through copying:

first = "Hello"
second = first
first += " World!"
// first == "Hello World!"
// second == "Hello"

String is a structure, therefore it's copied on assignment.

Structures also cannot be compared using identity operator:

window0 === window1 // works because a window is a class instance
"hello" === "hello" // error: binary operator '===' cannot be applied to two 'String' operands

Every two structure instances are deemed identical if they compare equal.

Collectively, these traits that differentiate structures from classes are what makes structures value types.



Got any Swift Language Question?