dimanche 23 octobre 2016

Swift 3: can't set optional Bool via reflection

I have the following code from a playground:

class Result :NSObject {
    var success :Bool? = false
    var message :String? = ""
}

var result = Result()
print(result.message)
var value :Bool? = true
result.setValue(value, forKey: "success")
result.setValue("hello", forKey: "message")
print(result.message)

This code fails on the first result.setValue line with the message "this class is not key value coding-compliant for the key success." If I make the property success non-optional then this code works fine. The property message works fine regardless of whether it's optional or not.

What's going on here? Is there any way around it?





Aucun commentaire:

Enregistrer un commentaire