NSMUTABLEARRAY- 항목을 교체하면 예외가 발생합니다… 도움말!
-
23-08-2019 - |
문제
나는 OBJ-C의 초보자입니다. 더 잘 배워야하므로 내가 뭘 잘못하고 있는지 말 해주세요 ..
이미지 배열이 있습니다 .... Exec의 다양한 지점에서 마지막 요소를 이전 이미지 중 하나로 바꿔야합니다 ... 마지막 이미지는 항상 이미지 중 하나를 복제합니다. 교체를 할 때 예외가 발생합니다! setCorrectImage에 대한 호출을 제거하면 작동합니다.
지난 몇 시간 동안 이것을 알아낼 수 없습니다 :-(
컨트롤러의 선언은 다음과 같습니다.
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];
}
해결책
imageSet = [NSArray arrayWithObjects:img, img1, img2, img3, img4, img5, img, nil];
당신은 만들고 있습니다 NSArray
(무조용 배열) 여기에 객체를 할당하고 imageSet
변하기 쉬운. 이것은 매우 나쁘기 때문입니다 imageSet
유형으로 선언되었습니다 NSMutableArray *
, 당신이 만든 객체는 유형이 있습니다 NSArray
, 그리고 NSArray
하위 유형이 아닙니다 NSMutableArray
.
따라서 오류는 실제로 객체가 NSArray
객체가 아닙니다 NSMutableArray
(또는 하위 클래스), 따라서 지원하지 않습니다. replaceObjectAtIndex:withObject:
메시지.
당신은 만들어야합니다 NSMutableArray
대신 객체 :
imageSet = [NSMutableArray arrayWithObjects:img, img1, img2, img3, img4, img5, img, nil];
제휴하지 않습니다 StackOverflow