Ich muss einen perform Selektor aus einem String implementieren. Der Selektor muss als Parameter einen Benachrichtigungswert haben.Implementieren Sie Selektor mit Parameter aus einem 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) {}
}
Ich glaube, ich bin etwas falsch zu machen, weil ich diesen Fehler hätte:
[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**
ich auch versucht:
func catchNotificationParagraphFinished_Chapter2(_ notification:Notification) {}
Und auch versucht, mit:
let name = "catchNotificationParagraphFinished_\(chapter.className!):"
let selector = Selector((name))
Ich basierte meinen Ansatz dank:
Ich muss wirklich meinen Selektor aus String erstellen. Abhängig von der Zeichenfolge werde ich catchNotificationParagraphFinished_Chapter2 oder catchNotificationParagraphFinished_Chapter3 ... etc aufrufen. Ich möchte es vermeiden, viele "if" -Bedingungen zu haben. – cmii
Ich sehe, ich habe meine Antwort bearbeitet, um dies zu reflektieren. – genghiskhan
Gleicher Absturz "[ISAMGAME.ChapterClass catchNotificationParagraphFinished_Chapter2 (Benachrichtigung :)]: unerkannter Selektor an Instanz gesendet" :( – cmii