facebookarchive/KVOController

method:"- (void)observe:(nullable id)object keyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options action:(SEL)action" assert selector in swift issue

ArthurXu424 opened this issue · 1 comments

I have code a NSObject SubCalss "GlobalVariables" in swift, show as below

import Cocoa

let Global = GlobalVariables()

class GlobalVariables: NSObject {
dynamic var LoginedAccount = String()

override init()
{
	super.init()
	LoginedAccount = String()
	self.setValue(LoginedAccount, forKeyPath: "LoginedAccount")
	
}

}

when i observe it in a ViewController with method "- (void)observe:(nullable id)object keyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options action:(SEL)action", assert error occurred. part of VC shows as below.

import Cocoa
class VC: NSViewController {
var observer:FBKVOController = FBKVOController.init(observer: Global)

@IBOutlet weak var LoginedAccountText: NSTextField!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.
	observer.observe(Global, keyPath: "LoginedAccount", options: .new, action: #selector(didLogined))
}
override func viewWillAppear() {
	observer.removeObserver(self.observer, forKeyPath: "LoginedAccount")
}
func didLogined()
{
	self.curAccount = Global.value(forKey: "LoginedAccount") as! String
	LoginedAccountText.cell?.title = curAccount
}

}

here is error message.
2017-06-19 16:00:10.724923+0800 App[43316:3926031] *** Assertion failure in -[FBKVOController observe:keyPath:options:action:], /....../FBKVOController.m:600
2017-06-19 16:00:10.734482+0800 App[43316:3926031] [General] <App.GlobalVariables: 0x60000025dd90> does not respond to didLogined
i tracked the code, stuck in method of FBKVOController.m ( line# 600) below
NSAssert([_observer respondsToSelector:action], @"%@ does not respond to %@", _observer, NSStringFromSelector(action));

My question is why it can not pass through?How can i fix this? What if i run in swift, Does your selector has any specific requirement?

Issue fixed, i use wrong argument to init KVO, it need to be 'self' which class you call KVO.