iOS [__NSArrayM insertObject:atIndex:]:对象不能为 nil - 太烦人了

Posted

技术标签:

【中文标题】iOS [__NSArrayM insertObject:atIndex:]:对象不能为 nil - 太烦人了【英文标题】:iOS [__NSArrayM insertObject:atIndex:]: object cannot be nil - So Annoying 【发布时间】:2013-06-16 01:22:05 【问题描述】:

这快把我逼疯了!

我看到了:

* 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'* -[__NSArrayM insertObject:atIndex:]: object cannot be nil' * 首先抛出调用栈: (0x28f9012 0x271ee7e 0x28acb6a 0x28aca20 0x5866c 0x5eeee 0x154f103 0x154f42b 0x155cf80 0x1564fad 0x156589b 0x1565e93 0x1565a88 0x8628 0x2732705 0x146f2c0 0x16aba64 0x2732705 0x146f2c0 0x146f258 0x1530021 0x153057f 0x152f6e8 0x149ecef 0x149ef02 0x147cd4a 0x146e698 0x2d76df9 0x2d76ad0 0x286ebf5 0x286e962 0x289fbb6 0x289ef44 0x289ee1b 0x2d757e3 0x2d75668 0x146bffc 0x3210 0x3175) libc++abi.dylib:终止调用抛出异常

第一个断点在 :

NSMutableArray *imgArray = [NSMutableArray array];
    IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
    subview.delegate = self;
    subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:0];
    UIImage *img1 = [[UIImage alloc] init];
    img1 = subview.photo.image;
    [imgArray addObject:img1]; //Breaking Here....

不确定这如何突然开始成为一个问题,我已恢复到早期版本,尝试了另一个工作站,但仍然在以下代码中看到此错误:

有什么想法吗? (不得不修剪文件因此突然结束)

@implementation IMReviewViewController 
    UIScrollView *scrollView;
    //UIPageControl *pageControl;
    BOOL continueButtonDisabled;
    NSUInteger downloadedImageCount;


@synthesize frameImage;

@synthesize removeButton;
@synthesize caption;
@synthesize delegate;
@synthesize photo = _photo;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    
    return self;


- (void)dealloc

    NSLog(@"dealloc - IMReviewViewController");


- (void)buttonPressed:(id)sender

    //    if (self.delegate && [self.delegate respondsToSelector:@selector(removePhoto:)]) 
    //        [self.delegate removePhoto:removePhoto:self.photo];
    //    

    for (UIView *view in scrollView.subviews) 
        [view removeFromSuperview];
    

    [self.navigationController popViewControllerAnimated:YES];


- (void)drawRect:(CGRect)rect

    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetShadow(context, CGSizeMake(3.0f, 3.0f), 2.0f);

    if (self.frameImage) 
        [self.frameImage drawInRect:rect];
    


- (void)didReceiveMemoryWarning

    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.


#pragma mark - View lifecycle

- (void)setReviewImages

    continueButtonDisabled = YES;
    downloadedImageCount = 0;

    NSArray *reviewViews = scrollView.subviews;
    for (IMReviewView *reviewView in reviewViews) 
        [reviewView removeFromSuperview];
    

    NSUInteger photoCount = [[IMLocalUser localUser] cachedPhotoCount];
    if ( nPageNum == 0 )
        for (NSUInteger i = 0; i < photoCount; i++) 

            CGRect frame;
            frame.origin.x  = scrollView.frame.size.width * i;
            frame.origin.y  = 65;
            frame.size      = CGSizeMake(scrollView.frame.size.width, 327.0f);

            IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
            subview.delegate = self;
            subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:i];
            [scrollView addSubview:subview];

            scrollView.showsHorizontalScrollIndicator = NO;
            scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * i, scrollView.frame.size.height);

            UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
            [self.view addSubview:headingLabel];


            headingLabel.text = @"Time To Preview!";
            headingLabel.textColor = [UIColor blackColor];
            headingLabel.textAlignment = UITextAlignmentCenter;

            headingLabel.textAlignment = NSTextAlignmentCenter;
            headingLabel.tag = 10;
            headingLabel.backgroundColor = [UIColor clearColor];
            headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
            headingLabel.hidden = NO;
            headingLabel.highlighted = YES;
            headingLabel.highlightedTextColor = [UIColor blackColor];
            headingLabel.lineBreakMode = YES;
            headingLabel.numberOfLines = 0;
        

        //  pageControl.numberOfPages = photoCoun
    else if( nPageNum == 1 )

        int nVWidth, nVHeight;
        nVWidth = self.view.frame.size.width;
        nVHeight = (self.view.frame.size.height - 310) / 2;

        CGRect frame;
        frame.origin.x  = 27.0f;
        frame.origin.y  = 65;
        frame.size      = CGSizeMake(269.5f, 253.5f);

        IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
        subview.delegate = self;
        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:0];
        UIImage *img1 = [[UIImage alloc] init];
        img1 = subview.photo.image;

        NSURL *imageURL = [[NSBundle mainBundle] URLForResource:@"FramedBack" withExtension:@"png"];
        frameImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];

        //Header Text above print

        UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
        [self.view addSubview:headingLabel];


        headingLabel.text = @"Time To Preview!";
        headingLabel.textColor = [UIColor blackColor];
        headingLabel.textAlignment = UITextAlignmentCenter;

        headingLabel.textAlignment = NSTextAlignmentCenter;
        headingLabel.tag = 10;
        headingLabel.backgroundColor = [UIColor clearColor];
        headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
        headingLabel.hidden = NO;
        headingLabel.highlighted = YES;
        headingLabel.highlightedTextColor = [UIColor blackColor];
        headingLabel.lineBreakMode = YES;
        headingLabel.numberOfLines = 0;


        //----------------
        UIGraphicsBeginImageContext(CGSizeMake(IMAGE_WIDTH, IMAGE_HEIGHT));
        CGSize divSize = CGSizeMake(IMAGE_WIDTH, IMAGE_HEIGHT);
        UIImage *image = [[UIImage alloc] init];

        image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
        //image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT)];


        image = img1;
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(300, 300, divSize.width-600, divSize.height-600)];

        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        UIImageView *viewImage = [[UIImageView alloc] initWithImage:newImage];
        [viewImage setFrame:frame];
        [scrollView addSubview:viewImage];

        NSURL *buottonImageURL = [[NSBundle mainBundle] URLForResource:@"RemoveIcon@2x" withExtension:@"png"];
        UIImage *buttonImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:buottonImageURL]];
        self.removeButton = [[UIButton alloc] initWithFrame:CGRectMake(13.0f, 50, 38.0f, 38.0f)];
        [self.removeButton setImage:buttonImage forState:UIControlStateNormal];
        [self.removeButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:self.removeButton];


        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
    else if( nPageNum == 2 )
        int nVWidth, nVHeight;
        nVWidth = self.view.frame.size.width;
        nVHeight = (self.view.frame.size.height - 330) / 2;

        CGRect frame;
        frame.origin.x  = 27.0f;
        frame.origin.y  = 65;
        frame.size      = CGSizeMake(269.5f, 253.5f);


        NSMutableArray *imgArray = [NSMutableArray array];
        IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
        subview.delegate = self;
        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:0];
        UIImage *img1 = [[UIImage alloc] init];
        img1 = subview.photo.image;
        [imgArray addObject:img1];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:1];
        UIImage *img2 = [[UIImage alloc] init];
        img2 = subview.photo.image;
        [imgArray addObject:img2];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:2];
        UIImage *img3 = [[UIImage alloc] init];
        img3 = subview.photo.image;
        [imgArray addObject:img3];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:3];
        UIImage *img4 = [[UIImage alloc] init];
        img4 = subview.photo.image;
        [imgArray addObject:img4];

        NSURL *imageURL = [[NSBundle mainBundle] URLForResource:@"FramedBack" withExtension:@"png"];
        frameImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];

        //Header Text above print

        UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
        [self.view addSubview:headingLabel];


        headingLabel.text = @"Time To Preview!";
        headingLabel.textColor = [UIColor blackColor];
        headingLabel.textAlignment = UITextAlignmentCenter;

        headingLabel.textAlignment = NSTextAlignmentCenter;
        headingLabel.tag = 10;
        headingLabel.backgroundColor = [UIColor clearColor];
        headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
        headingLabel.hidden = NO;
        headingLabel.highlighted = YES;
        headingLabel.highlightedTextColor = [UIColor blackColor];
        headingLabel.lineBreakMode = YES;
        headingLabel.numberOfLines = 0;

        //Footer Text under print

        UILabel *footer = [[UILabel alloc] initWithFrame:CGRectMake(50, 330, 226, 50)];
        [self.view addSubview:footer];

        footer.text = @"*Please note, you cannot select the layout of the images within the grid at this time.";
        footer.textColor = [UIColor blackColor];
        footer.textAlignment = UITextAlignmentCenter;

        footer.textAlignment = NSTextAlignmentCenter;
        footer.tag = 10;
        footer.backgroundColor = [UIColor clearColor];
        footer.font = [UIFont systemFontOfSize:9.0f];
        footer.hidden = NO;
        footer.highlighted = YES;
        footer.highlightedTextColor = [UIColor blackColor];
        footer.lineBreakMode = YES;
        footer.numberOfLines = 0;

        //----------------
        UIGraphicsBeginImageContext(CGSizeMake(IMAGE_WIDTH, IMAGE_HEIGHT));
        CGSize divSize = CGSizeMake(IMAGE_WIDTH / 2, IMAGE_HEIGHT / 2);
        UIImage *image = [[UIImage alloc] init];

        image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT)];


        image = [imgArray objectAtIndex:0];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(200, 200, divSize.width-210, divSize.height-210)];

        image = [imgArray objectAtIndex:1];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(divSize.width, 200, divSize.width-210, divSize.height-210)];

        image = [imgArray objectAtIndex:2];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(200, divSize.height, divSize.width-210, divSize.height-210)];

        image = [imgArray objectAtIndex:3];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(divSize.width, divSize.height, divSize.width-210, divSize.height-210)];

        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        UIImageView *viewImage = [[UIImageView alloc] initWithImage:newImage];
        [viewImage setFrame:frame];
        [scrollView addSubview:viewImage];

        NSURL *buottonImageURL = [[NSBundle mainBundle] URLForResource:@"RemoveIcon@2x" withExtension:@"png"];
        UIImage *buttonImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:buottonImageURL]];
        self.removeButton = [[UIButton alloc] initWithFrame:CGRectMake(13.0f, 50, 38.0f, 38.0f)];
        [self.removeButton setImage:buttonImage forState:UIControlStateNormal];
        [self.removeButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:self.removeButton];


        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
    else if( nPageNum == 3 )
        int nVWidth, nVHeight;
        nVWidth = self.view.frame.size.width;
        nVHeight = (self.view.frame.size.height - 243) / 2;
        CGRect frame;
        frame.origin.x  = 27.0f;
        frame.origin.y  = 65;
        frame.size      = CGSizeMake(269.5f, 253.5f);

        NSMutableArray *imgArray = [NSMutableArray array];
        IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
        subview.delegate = self;
        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:0];
        UIImage *img1 = [[UIImage alloc] init];
        img1 = subview.photo.image;
        [imgArray addObject:img1];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:1];
        UIImage *img2 = [[UIImage alloc] init];
        img2 = subview.photo.image;
        [imgArray addObject:img2];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:2];
        UIImage *img3 = [[UIImage alloc] init];
        img3 = subview.photo.image;
        [imgArray addObject:img3];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:3];
        UIImage *img4 = [[UIImage alloc] init];
        img4 = subview.photo.image;
        [imgArray addObject:img4];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:4];
        UIImage *img5 = [[UIImage alloc] init];
        img5 = subview.photo.image;
        [imgArray addObject:img5];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:5];
        UIImage *img6 = [[UIImage alloc] init];
        img6 = subview.photo.image;
        [imgArray addObject:img6];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:6];
        UIImage *img7 = [[UIImage alloc] init];
        img7 = subview.photo.image;
        [imgArray addObject:img7];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:7];
        UIImage *img8 = [[UIImage alloc] init];
        img8 = subview.photo.image;
        [imgArray addObject:img8];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:8];
        UIImage *img9 = [[UIImage alloc] init];
        img9 = subview.photo.image;
        [imgArray addObject:img9];

        NSURL *imageURL = [[NSBundle mainBundle] URLForResource:@"FramedBack" withExtension:@"png"];
        frameImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];

        //Header Text above print

        UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
        [self.view addSubview:headingLabel];


        headingLabel.text = @"Time To Preview!";
        headingLabel.textColor = [UIColor blackColor];
        headingLabel.textAlignment = UITextAlignmentCenter;

        headingLabel.textAlignment = NSTextAlignmentCenter;
        headingLabel.tag = 10;
        headingLabel.backgroundColor = [UIColor clearColor];
        headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
        headingLabel.hidden = NO;
        headingLabel.highlighted = YES;
        headingLabel.highlightedTextColor = [UIColor blackColor];
        headingLabel.lineBreakMode = YES;
        headingLabel.numberOfLines = 0;

        //Footer Text under print

        UILabel *footer = [[UILabel alloc] initWithFrame:CGRectMake(50, 330, 226, 50)];
        [self.view addSubview:footer];

        footer.text = @"*Please note, you cannot select the layout of the images within the grid at this time.";
        footer.textColor = [UIColor blackColor];
        footer.textAlignment = UITextAlignmentCenter;

        footer.textAlignment = NSTextAlignmentCenter;
        footer.tag = 10;
        footer.backgroundColor = [UIColor clearColor];
        footer.font = [UIFont systemFontOfSize:9.0f];
        footer.hidden = NO;
        footer.highlighted = YES;
        footer.highlightedTextColor = [UIColor blackColor];
        footer.lineBreakMode = YES;
        footer.numberOfLines = 0;

        //----------------
        UIGraphicsBeginImageContext(CGSizeMake(IMAGE_WIDTH, IMAGE_HEIGHT));
        CGSize divSize = CGSizeMake(IMAGE_WIDTH / 3, IMAGE_HEIGHT / 3);
        UIImage *image = [[UIImage alloc] init];

        image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT)];


        image = [imgArray objectAtIndex:0];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(210, 210, divSize.width-150, divSize.height-150)];

        image = [imgArray objectAtIndex:1];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(divSize.width + 70, 210, divSize.width-150, divSize.height-150)];

        image = [imgArray objectAtIndex:2];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(divSize.width * 2 - 70, 210, divSize.width-150, divSize.height-150)];

        image = [imgArray objectAtIndex:3];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(210, divSize.height + 70, divSize.width-150, divSize.height-150)];

        image = [imgArray objectAtIndex:4];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(divSize.width + 70, divSize.height + 70, divSize.width-150, divSize.height-150)];

        image = [imgArray objectAtIndex:5];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(divSize.width * 2 - 70, divSize.height + 70, divSize.width-150, divSize.height-150)];

        image = [imgArray objectAtIndex:6];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(210, divSize.height*2 -70, divSize.width-150, divSize.height-150)];

        image = [imgArray objectAtIndex:7];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(divSize.width + 70, divSize.height*2 -70, divSize.width-150, divSize.height-150)];

        image = [imgArray objectAtIndex:8];
        image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
        [image drawInRect:CGRectMake(divSize.width * 2 - 70, divSize.height*2 -70, divSize.width-150, divSize.height-150)];

        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        UIImageView *viewImage = [[UIImageView alloc] initWithImage:newImage];
        [viewImage setFrame:frame];
        [scrollView addSubview:viewImage];

        NSURL *buottonImageURL = [[NSBundle mainBundle] URLForResource:@"RemoveIcon@2x" withExtension:@"png"];
        UIImage *buttonImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:buottonImageURL]];
        self.removeButton = [[UIButton alloc] initWithFrame:CGRectMake(13.0f, 50, 38.0f, 38.0f)];
        [self.removeButton setImage:buttonImage forState:UIControlStateNormal];
        [self.removeButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:self.removeButton];


        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
    else if( nPageNum == 4 )
        int nVWidth, nVHeight;
        nVWidth = self.view.frame.size.width;
        nVHeight = (self.view.frame.size.height - 243) / 2;
        CGRect frame;
        frame.origin.x  = 27.0f;
        //        frame.origin.y  = 23.0f;
        frame.origin.y = 65;
        frame.size      = CGSizeMake(269.5f, 253.5f);

        NSMutableArray *imgArray = [NSMutableArray array];
        IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
        subview.delegate = self;
        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:0];
        UIImage *img1 = [[UIImage alloc] init];
        img1 = subview.photo.image;
        [imgArray addObject:img1];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:1];
        UIImage *img2 = [[UIImage alloc] init];
        img2 = subview.photo.image;
        [imgArray addObject:img2];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:2];
        UIImage *img3 = [[UIImage alloc] init];
        img3 = subview.photo.image;
        [imgArray addObject:img3];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:3];
        UIImage *img4 = [[UIImage alloc] init];
        img4 = subview.photo.image;
        [imgArray addObject:img4];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:4];
        UIImage *img5 = [[UIImage alloc] init];
        img5 = subview.photo.image;
        [imgArray addObject:img5];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:5];
        UIImage *img6 = [[UIImage alloc] init];
        img6 = subview.photo.image;
        [imgArray addObject:img6];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:6];
        UIImage *img7 = [[UIImage alloc] init];
        img7 = subview.photo.image;
        [imgArray addObject:img7];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:7];
        UIImage *img8 = [[UIImage alloc] init];
        img8 = subview.photo.image;
        [imgArray addObject:img8];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:8];
        UIImage *img9 = [[UIImage alloc] init];
        img9 = subview.photo.image;
        [imgArray addObject:img9];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:9];
        UIImage *img10 = [[UIImage alloc] init];
        img10 = subview.photo.image;
        [imgArray addObject:img10];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:10];
        UIImage *img11 = [[UIImage alloc] init];
        img11 = subview.photo.image;
        [imgArray addObject:img11];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:11];
        UIImage *img12 = [[UIImage alloc] init];
        img12 = subview.photo.image;
        [imgArray addObject:img12];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:12];
        UIImage *img13 = [[UIImage alloc] init];
        img13 = subview.photo.image;
        [imgArray addObject:img13];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:13];
        UIImage *img14 = [[UIImage alloc] init];
        img14 = subview.photo.image;
        [imgArray addObject:img14];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:14];
        UIImage *img15 = [[UIImage alloc] init];
        img15 = subview.photo.image;
        [imgArray addObject:img15];

        subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:15];
        UIImage *img16 = [[UIImage alloc] init];
        img16 = subview.photo.image;
        [imgArray addObject:img16];

【问题讨论】:

【参考方案1】:

既然报错就行了:

[imgArray addObject:img1];

这意味着img1nil。由于您从subview.photo.image 获得img1,这意味着以下三件事之一:

    subview.photo.imagenilsubview.photonilsubviewnil

既然您刚刚创建了subview,那么它可能不是#3。由于您从以下地址获得subview.photo

[[IMLocalUser localUser] cachedPhotoAtIndex:0];

您应该验证这是否返回一个非零值。如果它不是 nil,则验证 image 属性不是 nil

旁注:

这两行:

UIImage *img1 = [[UIImage alloc] init];
img1 = subview.photo.image;

创建一个浪费的UIImage。做吧:

UIImage *img1 = subview.photo.image;

【讨论】:

奇怪的是,有时它很好。其他的它会在 IMG4 时崩溃。

以上是关于iOS [__NSArrayM insertObject:atIndex:]:对象不能为 nil - 太烦人了的主要内容,如果未能解决你的问题,请参考以下文章

由于未捕获的异常“NSRangeException”而终止应用程序,原因:-[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围

如何提取 __NSArrayM 中的每个元素?

无法将“__NSArrayM”类型的值转换为“NSDictionary”

集合 <__NSArrayM: 0x7fa1f2711910> 在枚举时发生了突变

在映射集合时快速获取异常“所需类型 = NSOrderedSet;给定类型 = __NSArrayM”

[__NSArrayM objectAtIndex:]: 索引 9223372036854775807 超出范围 [0 .. 13]'