setValue:forKey总是迅速崩溃

我有以下用例:

class Foo: NSObject {
    var bar = ""
}

let foo = Foo()
foo.setValue("A name", forKey: "bar") //throws exception: this class is not key value coding-compliant for the key bar.
print("Foo.bar: \(foo.bar)")

Apple documentation (here) sais that in Swift each class subclasing NSObject becomes by default key value compliant. If so why am I getting not key value compliant exception?

从NSObject或其子类之一继承的Swift对象默认情况下对其属性遵循键值编码。

我想念什么吗?有人知道这可能是问题吗?

注意:我试图使“ bar”属性为NSString,但是出现了同样的异常。

评论