1

NSArrayの初期化と整数の追加に問題があります。これが私のコードで、私が達成しようとしていることについてコメントしました。オブジェクトを追加するとアプリがクラッシュしますが、配列を正しくクリアしているかどうかもわかりません。

//CREATE AND INITIALIZE AN ARRAY
NSArray *ticket;                                                        
ticket = [[NSArray alloc] init];

//slotA,slotB,slotC are of type NSInteger that I am trying to add
//to the array (THIS CRASHES)

[ticket addObject:[NSNumber numberWithInt:slotA]];
[ticket addObject:[NSNumber numberWithInt:slotB]];
[ticket addObject:[NSNumber numberWithInt:slotC]];

//I never got to this line of code but I think it has to be wrong
//because this would throw the whole //array away. I dont want it
//to be thrown away I just wanna clear it out but keep it instanciated.

[ticket release];

私はこれを試しましたが、「センチメンタル関数呼び出しがありません」と言っています

NSArray *ticket;
NSString *sltA=[NSString stringWithFormat:@"%d", slotA];
NSString *sltB=[NSString stringWithFormat:@"%d", slotB];
NSString *sltC=[NSString stringWithFormat:@"%d", slotC];
ticket = [[NSArray alloc] initWithObjects:sltA,sltB,sltC];

また、整数を文字列に変更して配列に入れる必要がありますか?

4

2 に答える 2

4

NSArrayをNSMutableArrayに変更します。これにより、オブジェクトを追加および削除できます。

NSArrayは、作成後に変更されることを想定していないアレイです。

NSMutableArrayはNSArrayの単なるサブクラスであり、配列内の任意のポイントでオブジェクトを追加および削除するのに役立つ多くの関数があります。

于 2010-07-06T02:17:25.107 に答える
-1

コードは正しいように見えます(技術的には、の+numberWithInteger:代わりに使用する必要があります+numberWithInt:が、クラッシュを引き起こすことはありません)。表示されるクラッシュは何ですか?GDB / Xcodeからの出力を投稿できますか?

于 2010-07-06T02:15:16.667 に答える