Skip to content

Instantly share code, notes, and snippets.

@somegeekintn
Created June 30, 2015 22:03
Show Gist options
  • Save somegeekintn/32cb4d335ae3244f33dc to your computer and use it in GitHub Desktop.
Save somegeekintn/32cb4d335ae3244f33dc to your computer and use it in GitHub Desktop.
Why does line 19 trigger didSet on stringProto?
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()
@somegeekintn
Copy link
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.

@bjtitus
Copy link

bjtitus commented Jul 1, 2015

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