Pergunta

Eu sou um novato em Obj-C. Eu preciso aprender isso melhor, então por favor me diga o que estou fazendo de errado ..

Eu tenho uma variedade de imagens .... Em vários pontos do executivo, preciso substituir o último elemento por uma das imagens anteriores ... então a última imagem sempre replica uma das imagens antes. Quando faço a substituição, ele lança uma exceção! Se eu remover a chamada para setCorrectImage, ele funciona.

Não consigo descobrir isso nas últimas horas agora :-(


A declaração no controlador.h é a seguinte-

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

A matriz é inicializada no controlador -

-(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];
}

Quando a vista aparece, é isso que acontece -

(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];
}
Foi útil?

Solução

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

Você está criando um NSArray (matriz não mutável) objeto aqui e atribuí-lo a seu imageSet variável. Isso é muito ruim porque imageSet foi declarado ser do tipo NSMutableArray *, e o objeto que você criou tem tipo NSArray, e NSArray não é um subtipo de NSMutableArray.

Portanto, o erro ocorre porque o objeto é realmente um NSArray objeto, não NSMutableArray (ou subclasse dela) e, portanto, não suporta um replaceObjectAtIndex:withObject: mensagem.

Você deve criar um NSMutableArray objeto em vez disso:

imageSet = [NSMutableArray arrayWithObjects:img, img1, img2, img3, img4, img5, img, nil];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top