struct XA {
static var xa = "Advanced"
var xb: String {
didSet {
XA.xa = oldValue
}
}}
var objXA = XA(xb: "Turing")
print(XA.xa) // Advanced
objXA.xb = "Swift"
print(XA.xa) // Turing
let objXB = XA(xb: "Quiz")
print(XA.xa) // Turing
i need to understand how these output coming in little bit depth. On the last line why its printing Turing not Swift.
2
Answers
I think that the reason is that, a non static value don’t have an access to a static value inside a same Class or Struct
The reason, as Joakim says in their comment, is that initializers do not invoke didSet/willSet. That’s by design.
To Quote the Apple Swift eBook (emphasis added by me)
Excerpt From
The Swift Programming Language (Swift 5.7)
Apple Inc.
https://books.apple.com/us/book/the-swift-programming-language-swift-5-7/id881256329
This material may be protected by copyright.