2016-11-17 86 views
0

在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'

enter image description here

我必須鍵入有我的Swift代碼工作?

編輯:使用通知中心,而不是通知

+1

只是爲了澄清。您的Objective C示例使用NSNotificationCenter,並且您的Swift代碼使用NSNotification。你想使用NSNotification還是NSNotificationCenter你想使用? – pbodsk

+0

事實上,我必須使用NotificationCenter。我編輯我的答案,但我仍然有同樣的錯誤。 –

回答

2

你應該調用的addObserver ..方法上default單 它應該是:

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

就是這樣,我在「addObserver」中使用了錯誤的句子。非常感謝您的時間 –

相關問題