سؤال

أنا مبتدئ في أوباج ج. أحتاج إلى تعلم هذا أفضل لذلك من فضلك قل لي ما أفعله خطأ ..

لدي مجموعة من الصور .... في نقاط 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];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top