2009-07-05 6 views
0

Je suis un débutant dans Obj-C. J'ai besoin d'apprendre cela mieux alors s'il vous plaît dites-moi ce que je fais mal ..NSMutableArray - le remplacement d'un élément provoque une exception ... aide!

J'ai un tableau d'images .... à divers points de exec je dois remplacer le dernier élément avec l'une des images précédentes. .. donc la dernière image reproduit toujours l'une des images avant. Quand je fais le remplacement, il jette une exception! Si je supprime l'appel à setCorrectImage, cela fonctionne.

Je ne suis pas en mesure de comprendre cela depuis quelques heures maintenant :-(


La déclaration du Controller.h est comme suit-

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

Le tableau est initialisé en le contrôleur -

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

Lorsque la vue semble que c'est ce qui se passe -

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

Répondre

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

Vous créez ici un objet NSArray (tableau non mutables) et l'assigner à votre variable imageSet. C'est très mauvais parce que imageSet a été déclaré comme étant de type NSMutableArray *, et l'objet que vous avez créé a le type NSArray, et NSArray n'est pas un sous-type de NSMutableArray. L'erreur se produit donc car l'objet est en fait un objet NSArray, et non NSMutableArray (ou sa sous-classe), et ne prend donc pas en charge le message replaceObjectAtIndex:withObject:.

Vous devez créer un objet NSMutableArray à la place:

imageSet = [NSMutableArray arrayWithObjects:img, img1, img2, img3, img4, img5, img, nil]; 
+0

Ah, je vous remercie! Je suis bête! C'est ce qui arrive si U regarde un morceau de code trop longtemps. C'était juste en face de moi et je ne le voyais pas ... merci. –

Questions connexes