无法将类型的价值“NSNotification.Name”预期参数类型“NSKeyValueObservingOptions”
问题描述:
在NMAPositioningManager.h有这个常量:无法将类型的价值“NSNotification.Name”预期参数类型“NSKeyValueObservingOptions”
FOUNDATION_EXPORT NSString *const NMAPositioningManagerDidUpdatePositionNotification;
而且有我的代码中迅速
NotificationCenter.addObserver(self, forKeyPath: "positionDidUpdate", options: NSNotification.Name.NMAPositioningManagerDidUpdatePosition, context: NMAPositioningManager.shared())
从Obj-C的这个例子中得到启发:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(positionDidUpdate)
name:NMAPositioningManagerDidUpdatePositionNotification
object:[NMAPositioningManager sharedNMAPositioningManager]];
我对字段选项有一个错误:
NavigationViewController.swift:30:84: 不能类型的值 'NSNotification.Name' 转换为预期 参数类型 'NSKeyValueObservingOptions'
我必须键入有我的Swift代码工作?
编辑:使用通知中心,而不是通知
答
你应该调用的addObserver ..方法上default
单 它应该是:
NotificationCenter.default.addObserver(self, selector: #selector(positionDidUpdate), name: NSNotification.Name.NMAPositioningManagerDidUpdatePosition, object: NMAPositioningManager.shared())
+0
就是这样,我在“addObserver”中使用了错误的句子。非常感谢您的时间 –
只是为了澄清。您的Objective C示例使用NSNotificationCenter,并且您的Swift代码使用NSNotification。你想使用NSNotification还是NSNotificationCenter你想使用? – pbodsk
事实上,我必须使用NotificationCenter。我编辑我的答案,但我仍然有同样的错误。 –