I'm testing Swift 2.0 beta right now and have found strange behaviour. Here is a sample code:
private func someFunc(inout someString: String) {
print("Inside 'someFunc()'")
print(someString)
someString = "Some another string"
}
private var someAncillaryInt = 42
print(someAncillaryInt)
private var someString: String {
get {
print("Inside 'getter'")
return "Some string"
}
set {
print("Inside 'setter'")
someAncillaryInt = 24
}
}
someFunc(&someString)
print(someAncillaryInt)
Output:
42
Inside 'getter'
Inside 'someFunc()'
Some string
Inside 'setter'
24
I don't understand why wasn't getter called while printing someString
inside someFunc()
and why was it when someFunc()
got passed with someString
.
One can assume that I don't understand intricacies of inout parameters yet and after being passed as inout parameter computed property stops being, em, "computed", but why then was 'setter' called when we set another value to someString
?
Thanks!
UPD: I added answer below.
UPDATE 18/11/2015: Apple has updated their manual with detailed explanation of how inout params work.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…