NotificationCenter Crash in Swift 3
Asked Answered
D

1

11

Is it just me, or did NotificationCenter become a hot mess in Swift 3? :)

I have the following setup:

// 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)

In my first view controller, this works great:

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

func refreshData() {
  // ...
}

But this view controller:

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

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

...crashes with:

[NSConcreteNotification length]: unrecognized selector sent to instance

As far as I can tell, my observer is set up correctly. Any idea what I'm doing wrong?

Dorkas answered 17/11, 2016 at 0:18 Comment(1)
Possible duplicate of NSNotifications in Swift 3Conformable
B
10

Your issue is with your loadEntries(search:) method. It's not a valid signature. The selector used with Notification Center must either have no parameters or just one parameter. And if you have one parameter, that parameter will be the Notification object, not the notification name.

Your loadEntries needs to be:

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

And the selector would need to be:

#selector(loadEntries(_:)) // or #selector(loadEntries)
Betweentimes answered 17/11, 2016 at 0:29 Comment(1)
Ah, I see. Thanks!Dorkas

© 2022 - 2024 — McMap. All rights reserved.