December 2018
Intermediate to advanced
414 pages
10h 19m
English
Now, let's try to port our Point class into a struct. Consider the following:
struct Point { var x: Double var y: Double}
We have defined a simple struct; as you should notice, there's no need to add a constructor, as the compiler will synthesize it for us:
let point = Point(x: 0.0, y: 0.0)translate(point: point, dx: 1.0, dy: 1.0)
If we keep the original implementation, our program won't compile. Point is a value type now, and it's forbidden to mutate a value inside of a function! We need to add the inout keyword to indicate that this function will mutate the contents of the value that is passed. When the function returns, the value will be assigned back to the original variable.
With those changes complete, we also need to change ...
Read now
Unlock full access