I want to use Lazy initialization for some of my properties in Swift. My current code looks like this:
lazy var fontSize : CGFloat = {
if (someCase) {
return CGFloat(30)
} else {
return CGFloat(17)
}
}()
The thing is that once the fontSize is set it will NEVER change. So I wanted to do something like this:
lazy let fontSize : CGFloat = {
if (someCase) {
return CGFloat(30)
} else {
return CGFloat(17)
}
}()
Which is impossible.
Only this works:
let fontSize : CGFloat = {
if (someCase) {
return CGFloat(30)
} else {
return CGFloat(17)
}
}()
So - I want a property that will be lazy loaded but will never change.
What is the correct way to do that? using let
and forget about the lazy init? Or should I use lazy var
and forget about the constant nature of the property?
lazy
you can get is withvar
(as of Swift 2.1), if it's only a float assignment I wouldn't worry about the lazy, especially if you absolutely know it will be accessed – Trawl