3

それは私だけですか、またはNotificationCenterはスウィフト3の熱い混乱になったのですか? :)私は次のセットアップがあります。私の最初のビューコントローラで通知センタークラッシュスウィフト3

//Yonder.swift 
extension Notification.Name { 
    static let preferenceNotification = Notification.Name("preferencesChanged") 
} 

//I fire the notification elsewhere like this: 
NotificationCenter.default.post(name: .preferenceNotification, object: nil) 

を、これは素晴らしい作品:

//View Controller A <-- Success! 
NotificationCenter.default.addObserver(self, selector: #selector(refreshData), name: .preferenceNotification, object: nil) 

func refreshData(){ 
    //... 
} 

しかし、このビューコントローラ:

//View Controller B <-- Crash :(
NotificationCenter.default.addObserver(self, selector: #selector(loadEntries(search:)), name: .preferenceNotification, object: nil) 

func loadEntries(search:String?){ 
    //... 
} 

...でクラッシュ:

[NSConcreteNotification length]:認識できないセレクタ例として

私の知る限り、私の観察者は正しく設定されています。私が間違っていることは何か考えていますか?

+0

可能な複製(http://stackoverflow.com/questions/38310080/nsnotifications-in-swift-3) –

答えて

3

あなたの問題はloadEntries(search:)メソッドにあります。それは有効な署名ではありません。通知センターで使用されるセレクタには、パラメータがないか、1つのパラメータのみが必要です。 1つのパラメータがある場合、そのパラメータは通知名ではなくNotificationオブジェクトになります。

あなたloadEntriesをする必要があります:

func loadEntries(_ notification: NSNotification) { 
    // Optional check of the name 
    if notification.name == .preferenceNotification { 
    } 
} 

セレクタがする必要があります:[スウィフト3でNSNotifications]の

#selector(loadEntries(_:) 
+0

ああ、そうです。ありがとう! –