2017-07-12 5 views
0

Stringからperformセレクタを実装する必要があります。 セレクタは、パラメータとして通知値を持たなければなりません。Stringからパラメータを指定してperformセレクタを実装する

[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)) 
私はこのエラーを得たので

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) {} 
    } 

私は、私が何か間違ったことをやっていると仮定します

私のアプローチは以下の通りです:

答えて

0

私の方法はうまくいきますが、Sを追加することを忘れないでください定数に型を変換する...

let name:String = "catchNotificationParagraphFinished_\(chapter.className!):" 
0

私は、したがって、セレクタ文字列となり、あなたはセレクタ文字列でパラメータ名だけでなく、クラス名を含める必要が信じている:

let name = "ChapterClass.catchNotificationParagraphFinished_\(chapter.className!)(notification:)" 
+0

私は本当にStringからセレクタを作成する必要があります。文字列に応じて、私はcatchNotificationParagraphFinished_Chapter2またはcatchNotificationParagraphFinished_Chapter3などを呼び出します...私は多くの "if"条件を持つことを避けたいと思います。 – cmii

+0

私は、これを反映するために私の答えを編集しています。 – genghiskhan

+0

同じクラッシュ "[ISAMGAME.ChapterClass catchNotificationParagraphFinished_Chapter2(notification :)]:インスタンスに送信されたセレクタが認識されません" :( – cmii

関連する問題