大家好我想要在Singleton類中的一個字符串屬性上實現KVO。當我嘗試添加觀察者時,我正遇到一些錯誤,並希望有人能告訴我我做錯了什麼。 下面顯示了我的單身人士課程。單身屬性的KVO?
class User: NSObject {
static let currentUser = User()
private override init() {}
var pictureString: String?
}
在單獨的ViewController的viewDidLoad中,我試圖添加一個觀察者到像這樣的pictureString變量。
self.addObserver(self, forKeyPath: #keyPath(User.currentUser.pictureString), options: [.old, .new, .initial], context: nil)
我現在正在處理的問題是,它指出currentUser不符合KVC。我目前正在獲取下面的錯誤。
addObserver: forKeyPath:@"currentUser.pictureString" options:7 context:0x0] was sent to an object that is not KVC-compliant for the "currentUser" property.'
我很感激任何幫助謝謝。
是因爲'profilePic'屬性在'User'類中不存在嗎? – Rishab
我想你想使用'pictureString',而不是'profilePic' – Tj3n
oops editted。我仍然有同樣的錯誤,無論 – victorydub