2012-04-09 4 views
2

はまあ、私のようなカスタムSEL作成しています:objective-C:performSelectorの実行方法:@セレクタ?

NSArray *tableArray = [NSArray arrayWithObjects:@"aaa", @"bbb", nil]; 
for (NSString *table in tableArray){ 
    SEL customSelector = NSSelectorFromString([NSString stringWithFormat:@"abcWith%@", table]); 
    [self performSelector:customSelector withObject:0]; 
} 

を私はエラーを得た: がキャッチされない例外により「NSInvalidArgumentException」、理由にアプリを終了:「 - [同期aaaWithaaa]:認識されていないセレクタがに送信されましたインスタンス

実際のメソッド名で実行した場合、動作します。

[self performSelector:@selector(aaaWithaaa:) withObject:0]; 

どうすれば解決できますか?ありがとう!

あなたはすでに文字列からセレクタを作成しました

答えて

6

は - performSelectorに渡す:方法:

[self performSelector:customSelector withObject:0]; 

編集:マインド、あなたのメソッドは、パラメータを取る場合のセレクタを作成する際には、コロンを使用しなければならないことをそれ:

// Note that you may need colon here: 
[NSString stringWithFormat:@"abcWith%@:", table] 
+0

ありがとうございます、私の質問 –

+0

を更新しましたあなたの権利がある!コロン! –

0

閉じる。

@selector(aaaWithaaa:)ではメソッド名を渡しますが、@selector(customSelector:)では、SEL型の変数(スペアコロン)を渡しています。

は代わりに、あなただけの必要があります。

[self performSelector:customSelector withObject:0]; 

を他の違いは、あなたが最後にコロンとあなたの文字列を書くということですが、あなたstringWithFormat:は何を持っていません。それは重要です。メソッドがパラメータを取ることを意味します。あなたのメソッドは、パラメータを持っている場合、それはそこにする必要があり、すなわち、

[NSString stringWithFormat:@"abcWith%@:", table] 
+0

ありがとうございました、私の質問を更新しました –

1
NSArray *tableArray = [NSArray arrayWithObjects:@"aaa", @"bbb", nil]; 

for (NSString *table in tableArray){ 
    SEL customSelector = NSSelectorFromString([NSString stringWithFormat:@"abcWith%@:", table]); 
    [self performSelector:customSelector withObject:0]; 
} 
0
- (id)performSelector:(SEL)aSelector withObject:(id)anObject 

最初の引数はSELタイプです。

SEL customSelector = NSSelectorFromString([NSString stringWithFormat:@"abcWith%@", table]); 
[self performSelector:customSelector withObject:0]; 
関連する問題