Created
June 30, 2015 22:03
-
-
Save somegeekintn/32cb4d335ae3244f33dc to your computer and use it in GitHub Desktop.
Why does line 19 trigger didSet on stringProto?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol StringProto { | |
| var string: String? { get set } | |
| } | |
| class StringClass : StringProto { | |
| var string : String? = "" | |
| } | |
| class SetterTest { | |
| var stringProto: StringProto? { | |
| didSet { | |
| println("didSet") | |
| self.initStringVal() | |
| } | |
| } | |
| func initStringVal() { | |
| println("initStringVal") | |
| test.stringProto?.string = "init value" | |
| } | |
| } | |
| var test = SetterTest() | |
| test.stringProto = StringClass() |
Author
Author
And the answer is to change line 2 to: var string: String? { get nonmutating set } as the Protocol doesn't know if it's dealing with a value type.
Also, can have StringProto inherit from class
protocol StringProto: inherit class {
var string: String? { get set }
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also if stringProto is declared as
var stringProto: StringClass?didSetwill not be called byinitStringVal