是因为我吗,还是NotificationCenter成了Swift 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?) {
// ...
}...crashes,包括:
NSConcreteNotification长度:发送到实例的不可识别的选择器
据我所知,我的观察者的设置是正确的。知道我做错什么了吗?
发布于 2016-11-17 00:29:42
您的问题在于您的loadEntries(search:)方法。这不是有效的签名。与Notification一起使用的选择器必须没有参数或只有一个参数。如果有一个参数,则该参数将是Notification对象,而不是通知名称。
您的loadEntries必须是:
func loadEntries(_ notification: NSNotification) {
// Optional check of the name
if notification.name == .preferenceNotification {
}
}选择者必须是:
#selector(loadEntries(_:)) // or #selector(loadEntries)https://stackoverflow.com/questions/40644703
复制相似问题