Lazy loading property in Extension (Swift)
you can use computed property, combined with associatedObject. in this way, you can mimic a stored property. so the lazy var simulation will be:
// global var's address used as key
var #PropertyKey# : UInt8 = 0
var #varName# : #type# {
get {
if let v = objc_getAssociatedObject(self, & #PropertyKey#) as #type# {
return v
}else {
// the val not exist, init it and set it. then return it
// this way doesn't support nil option value.
// if you need nil option value, you need another associatedObject to indicate this situation.
}
}
set {
objc_setAssociatedObject(self, & #PropertyKey#, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
}
}
If you don't need to refer to self
you can use a static var
:
extension String {
static var count = 0
static var laughingOutLoud : String = {
count++
return "LOL: \(count)"
}()
}
String.laughingOutLoud // outputs "LOL: 1"
String.laughingOutLoud // outputs "LOL: 1"
String.laughingOutLoud // outputs "LOL: 1"
(You don't need count
in your implementation; that's just there to show it's only executed once.)