Swift: 为什么本地化有时需要添加观察者才能使其工作但有时不需要

Swift: Why localization sometime need to add observer to get it work but sometime don't

我已经开发了一个需要使用本地化的应用程序,所以我为我的应用程序选择了 this nice library。但我只是混淆了他们的文档中提到他们需要使用观察者通知来收听语言何时发生变化。但是在我的主控制器中,我只是按照他们说的去做,它就像一个魅力一样工作,但是当我将本地化添加到我的侧边栏文本时。我收到此错误:

原因:'-[NSConcreteNotification 长度]:无法识别的选择器发送到实例 0x107cbb9f0'

但是如果我删除侧边栏控制器内的观察者。它会工作正常。 所以我的问题是为什么有些控制器需要添加一个观察者才能工作,而有些则不需要添加它来工作

这是 MySidebar 控制器:

override func viewWillAppear(_ animated: Bool) {
         NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)     
    }
deinit {
        NotificationCenter.default.removeObserver(self)
    }
@objc func setText(status: String) {
   item = "item".localized(using: "NavigationDrawerViewController")
}

这是 ChooseLanguageController:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
        Localize.setCurrentLanguage(availableLanguages[indexPath.row])
        mytable.reloadData()
    }

如果我的英语看起来很混乱,请提前道歉

选择器的签名必须将 Notification 作为参数而不是 String。像这样

@objc func setText(notification: Notification) {

}

来自文档

Selector that specifies the message the receiver sends observer to notify it of the notification posting. The method specified by aSelector must have one and only one argument (an instance of NSNotification).

有关详细信息,请参阅 addObserver(_:selector:name:object:)