0

私は Obj-C の初心者です。私はこれをもっとよく学ぶ必要があるので、私が間違っていることを教えてください..

私は画像の配列を持っています....実行のさまざまな時点で、最後の要素を前の画像の1つに置き換える必要があります...したがって、最後の画像は常に前の画像の1つを複製します。置換を行うと、例外がスローされます! setCorrectImage への呼び出しを削除すると、機能します。

ここ数時間、これを理解することができません:-(


controller.h の宣言は次のとおりです。

NSMutableArray      *imageSet;
UIImage *img, *img1, *img2, *img3, *img4, *img5;

アレイはコントローラで初期化されます -

-(void)loadStarImageSet
{

    NSString *imagePath = [[NSBundle mainBundle] pathForResource:AWARD_STAR_0 ofType:@"png"], 
    *imagePath1 = [[NSBundle mainBundle] pathForResource:AWARD_STAR_1 ofType:@"png"],
    *imagePath2 = [[NSBundle mainBundle] pathForResource:AWARD_STAR_2 ofType:@"png"],
    *imagePath3 = [[NSBundle mainBundle] pathForResource:AWARD_STAR_3 ofType:@"png"],
    *imagePath4 = [[NSBundle mainBundle] pathForResource:AWARD_STAR_4 ofType:@"png"],
    *imagePath5 = [[NSBundle mainBundle] pathForResource:AWARD_STAR_5 ofType:@"png"]    
    ;

    img  = [[UIImage alloc] initWithContentsOfFile:imagePath];
    img1 = [[UIImage alloc] initWithContentsOfFile:imagePath1];
    img2 = [[UIImage alloc] initWithContentsOfFile:imagePath2];
    img3 = [[UIImage alloc] initWithContentsOfFile:imagePath3];
    img4 = [[UIImage alloc] initWithContentsOfFile:imagePath4];
    img5 = [[UIImage alloc] initWithContentsOfFile:imagePath5];


    if(imageSet != nil)
    {
        [imageSet release];
    }
    imageSet = [NSArray arrayWithObjects:img, img1, img2, img3, img4, img5, img, nil];

    [imageSet retain];
}

ビューが表示されると、これが起こります-

(void)viewDidAppear:(BOOL)animated
{
    [self processResults];

    [self setCorrectImage];

    [self animateStar];
}


-(void)setCorrectImage
{
    // It crashes on this assignment below!!!!!

    [imageSet replaceObjectAtIndex:6 withObject:img4]; // hard-coded img4 for prototype... it will be dynamic later
}

-(void) animateStar
{
    //Load the Images into the UIImageView var - imageViewResult
    [imageViewResult setAnimationImages:imageSet];

    imageViewResult.animationDuration = 1.5;
    imageViewResult.animationRepeatCount = 1;
    [imageViewResult startAnimating];
}
4

1 に答える 1

2
imageSet = [NSArray arrayWithObjects:img, img1, img2, img3, img4, img5, img, nil];

NSArrayここで (非可変配列) オブジェクトを作成し、それをimageSet変数に割り当てています。imageSetは タイプ であると宣言さNSMutableArray *れており、作成したオブジェクトのタイプは であり、 のサブタイプではないため、NSArrayこれは非常に悪いことです。NSArrayNSMutableArray

したがって、オブジェクトが実際にはNSArrayオブジェクトNSMutableArray(またはそのサブクラス) ではなく、replaceObjectAtIndex:withObject:メッセージをサポートしていないため、エラーが発生します。

NSMutableArray代わりにオブジェクトを作成する必要があります。

imageSet = [NSMutableArray arrayWithObjects:img, img1, img2, img3, img4, img5, img, nil];
于 2009-07-05T04:36:32.280 に答える