4

値を変更する必要がある NSMutableArray がありますが、次のエラーが発生します:
[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x5291db0
これは私の NSMutableArray の宣言です:

NSMutableArray *selectedOptions = [NSArray arrayWithObjects:[NSNumber numberWithInteger:0], nil]; 

次に、次のように replaceObjectAtIndex メソッドを使用しています。

[self.selectedOptions replaceObjectAtIndex:0 withObject:[NSNumber numberWithInteger:1]];

しかし、そのエラーが発生し、NSMutableArray を使用しています。
ありがとう

4

2 に答える 2

9

通常の非可変を作成していNSArrayます。あなたのコードは

NSMutableArray *selectedOptions = [NSMutableArray arrayWithObjects:[NSNumber numberWithInteger:0], nil]; 

Objective C は非常に動的であるため、コンパイル時にこの間違いを検出しません。

于 2013-01-17T02:12:14.897 に答える
6

NSMutableArrayを実行して初期化する必要があります

NSMutableArray *selectedOptions = [NSMutableArray alloc] init];

で初期化するとNSArray、メソッドを使用できなくなり、それがrepalceObjectAtIndex:withObject:問題の原因です。

上記の行であなたNSMutableArrayを初期化した後、メソッドでオブジェクトを追加するだけですaddObject

于 2013-01-17T02:11:50.453 に答える