我需要从字符串实现一个执行选择器。 选择器必须有一个通知值作为参数。
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]: unrecognized selector sent to instance 0x600000052c60
*Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ISAMGAME.ChapterClass catchNotificationParagraphFinished_Chapter2]: unrecognized selector sent to instance 0x600000052c60**
我也尝试过:
func catchNotificationParagraphFinished_Chapter2(_ notification:Notification) {}
还尝试使用:
let name = "catchNotificationParagraphFinished_\(chapter.className!):"
let selector = Selector((name))
我的方法基于:
最佳答案
我相信您必须在选择器字符串中包含参数名称和类名称,因此选择器字符串变为:
let name = "ChapterClass.catchNotificationParagraphFinished_\(chapter.className!)(notification:)"
关于ios - 使用字符串参数实现执行选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45057915/