Swift Structs

Swift Structs

Define lightweight value types with stored properties and memberwise initializers.

Structs are copied on assignment and passing.


Define a Struct

Create a struct with stored properties and memberwise initializers, then instantiate and call methods on the instance.

Example

struct Point { var x: Int; var y: Int }
var p1 = Point(x: 1, y: 2)
var p2 = p1 // copy
p2.x = 10

Structs are copied on assignment and passing, so mutations on one reference do not affect the other.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *