objective-c - 无法将类型 'NSNotification.Name' 的值转换为预期的参数类型 'NSKeyValueObservingOptions'

标签 objective-c swift nsnotificationcenter here-api

在 NMAPositioningManager.h 中有这个常量:

FOUNDATION_EXPORT NSString *const NMAPositioningManagerDidUpdatePositionNotification;

我的代码在 swift 中

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: Cannot convert value of type 'NSNotification.Name' to expected argument type 'NSKeyValueObservingOptions'

enter image description here

我必须输入什么才能使我的 Swift 代码正常工作?

编辑:使用 NotificationCenter 而不是 Notification

最佳答案

你应该在 default 单例上调用 addObserver.. 方法 应该是:

NotificationCenter.default.addObserver(self, selector: #selector(positionDidUpdate), name: NSNotification.Name.NMAPositioningManagerDidUpdatePosition, object: NMAPositioningManager.shared())

关于objective-c - 无法将类型 'NSNotification.Name' 的值转换为预期的参数类型 'NSKeyValueObservingOptions',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40652359/

相关文章:

ios - 在 NSMutableArray 中过滤

ios - 在核心数据 swift 中使用 one -> may 关系保存数据集合

iOS - 使用动画展开/折叠 UITableView 部分

objective-c - 解析通知 userInfo 时出现 NSString IntValue 异常

ios - UIViewController 收到通知并在按下按钮时推送 segues,但需要防止多次按下按钮和 segues

iphone - iOs 设备特定资源不工作 - ~iPad 加载默认 XIB - 通用应用程序

objective-c - 将 UITableView 和数据源/委托(delegate)放在单独的类中不起作用 (XCode 4.2)

objective-c - 如何给 NSText 多个阴影?

swift - 在 Swift 4 中访问前置摄像头

ios - 用于位置更新多个 View 的单例