我需要从String实现一个执行选择器。 选择器必须具有通知值作为参数。
class ChapterViewController: UIViewController {
var chapterClass:ChapterClass!
func catchNotificationParagraphFinished(notification:Notification) {
let name = "catchNotificationParagraphFinished_\(chapter.className!)"
let selector = NSSelectorFromString(name)
chapterClass.perform(selector, with: notification)
}
}
class ChapterClass: NSObject {
func catchNotificationParagraphFinished_Chapter2(notification:Notification) {}
}
我想我做错了什么,因为我收到了这个错误:
[ISAMGAME.ChapterClass catchNotificationParagraphFinished_Chapter2]:无法识别的选择器发送到实例0x600000052c60
*由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [ISAMGAME.ChapterClass catchNotificationParagraphFinished_Chapter2]:无法识别的选择器 发送到实例0x600000052c60 **
我也尝试过:
func catchNotificationParagraphFinished_Chapter2(_ notification:Notification) {}
并尝试使用:
let name = "catchNotificationParagraphFinished_\(chapter.className!):"
let selector = Selector((name))
我的方法归功于:
答案 0 :(得分:0)
我认为你必须在选择器字符串中包含参数名称和类名,因此选择器字符串变为:
let name = "ChapterClass.catchNotificationParagraphFinished_\(chapter.className!)(notification:)"
答案 1 :(得分:0)
我的方法工作正常,但不要忘记将String类型添加到常量...
broadcast receiver