'Is it correct to convert from computed property to lazy variable under constant struct?
I wanted to change my code to use lazy variable instead of Computed property.
this code works
struct Point {
var x = 0.0, y = 0.0
}
struct Size {
var width = 10.0, height = 10.0
}
struct Rect {
var origin = Point()
var size = Size()
var center: Point{
get{
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
}}
let shape = Rect()
print(shape.center)
But this code produces an error
struct Point {
var x = 0.0, y = 0.0
}
struct Size {
var width = 10.0, height = 10.0
}
struct Rect {
var origin = Point()
var size = Size()
lazy var center = Point(x: origin.x + (size.width / 2), y: origin.y + (size.height / 2))
}
let shape = Rect()
print(shape.center)
I can fix the error if I changed let shape = Rect() to variable type var shape = Rect() OR when I change Rect to class instead of struct
What is wrong with my code and why does it give me error?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
