1

合成されたNSMutableArray--theResultArrayがあります。NSNumberまたはNSIntegerオブジェクトを特定のインデックス(0〜49)に挿入したい。何らかの理由で、配列に固定する値を取得できません。すべてのインデックスはnilまたは0を返します。

    NSInteger timeNum = time;
    [theResultArray insertObject:[NSNumber numberWithInt:timeNum] atIndex:rightIndex];
    NSLog(@"The right index is :%i", rightIndex);
    NSLog(@"The attempted insert time :%i", time);
    NSNumber *testNum = [theResultArray objectAtIndex:rightIndex];
    NSLog(@"The result of time insert is:%i", [testNum intValue]);

viewDidLoadでtheResultsArrayを割り当てて初期化します。時間は整数です。私は上記のコードのさまざまな組み合わせを試しましたが、役に立ちませんでした。

コンソールはこれを出力します:

StateOutlineFlashCards[20389:20b] The right index is :20
StateOutlineFlashCards[20389:20b] The attempted insert time :8
StateOutlineFlashCards[20389:20b] The result of time insert is:0
4

3 に答える 3

5

読み間違えない限り、NSIntegerを挿入してから、NSNumberを取り出そうとしているのではないでしょうか。これらは2つの完全に異なるデータ型です。あなたが奇妙な結果を得ていることは私を驚かせません。

さらに、NSIntegerはオブジェクトではないため、配列に貼り付けることはできません。おそらく、NSNumberにその整数を割り当てて、それを入れたいと思うでしょう。

次のようなものを試してください: [theResultArray addObject:[NSNumber numberWithInteger:timeNum] atIndex:rightIndex];

同様に、値を取得するときは、ボックスを解除する必要があります。

NSLog(@"The result of time insert is:%i", [testNum integerValue])`;

同様に、値を取得するときは、ボックスを解除する必要があります。

率直に言って、これがコンパイルされることに少し驚いています。

于 2009-08-18T17:04:31.510 に答える
4

initまたはviewDidLoadメソッドで配列にメモリを割り当てる必要があります。そうしないと、何も保存できなくなります。

これを行う場合:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization        
        myMutableArrayName = [[NSMutableArray alloc] init];
    }
    return self;
}

またはこれ:

- (void)viewDidLoad {
    [super viewDidLoad];
    myMutableArrayName = [[NSMutableArray alloc] init];
}

それはあなたのために働くはずです。

NSMutableArrayに整数を格納することに関しては、最近、単純ですがやや「ハック」なアプローチを取りました。文字列として保存します。私がそれらを入れるとき、私は使用します:

[NSString stringWithFormat:@"%d", myInteger];

そして、私がそれらを取り出すとき、私は次のように変換します:

[[myArray objectAtIndex:2] intValue];

実装は本当に簡単でしたが、コンテキストによっては別の方法を使用したい場合があります。

于 2009-08-18T17:27:37.097 に答える
1
NSInteger timeNum = time;

それは何のためにあるのです?何時ですか"?

    [theResultArray addObject:timeNum atIndex:rightIndex];

メソッド-addObject:atIndex:はありません。-insertObject:atIndex:です。とにかく「rightIndex」に挿入するのはなぜですか?-addObject:を使用しないのはなぜですか?

    //[theResultArray replaceObjectAtIndex:rightIndex withObject:[NSNumber numberWithInt:timeNum]];

それは何ですか、なぜコメントアウトされているのですか?

    NSLog(@"The right index is :%i", rightIndex);
    NSLog(@"The attempted insert time :%i", time);
    NSNumber *testNum = [theResultArray objectAtIndex:rightIndex];
    //int reso = [testNum integerValue];
    NSLog(@"The result of time insert is:%i", testNum);

あなたは何をしようとしているのですか?

于 2009-08-18T17:14:01.053 に答える