我有一个带有绑定值的NSTextField - 绑定到委托“self.myObject.Title” 运行此代码
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
var myObject = OwnObject()
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
myObject = OwnObject()
print(myObject.Title)
}
}
class OwnObject: NSObject {
var Title : String! = ""
override init() {
super.init()
self.Title = "This is a title"
}
}
将填充TextField的正确标题,但myObject将自动解除分配,我收到此错误
2015-06-16 15:43:55.587 testapp109[7896:463502] An instance 0x60000004d4d0 of class testapp109.OwnObject was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x6080000538c0> (
<NSKeyValueObservance 0x6080000c3aa0: Observer: 0x6080000c3a30, Key path: Title, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x608000053890>
)
为什么对象被解除分配,我该如何防止这种情况? 感谢