NSArray เป็นโมฆะเมื่อฉันสร้างมันขึ้นมาในขณะที่ nsarray แรกไม่เป็นโมฆะ

นี่คือรหัสของฉันพยายามสร้างอาร์เรย์ NSMutable ด้วยสตริง จากนั้นจัดเก็บไว้ในคุณสมบัติของวัตถุ NSArray *photos ใช้งานได้ แต่ไม่ใช่ NSMUtableArray thumbImageURL เมื่อฉัน NSLog เพื่อจุดประสงค์ในการแก้ไขจุดบกพร่อง มันจะถือเป็นโมฆะ โปรดช่วยด้วย มันทำให้ฉันรำคาญมาก ไม่สามารถหาวิธีแก้ไขได้ ฉันขี้เกียจสร้างอินสแตนซ์ด้วยดังนั้นจึงไม่มีเหตุผลที่จะไม่มีการจัดสรรในหน่วยความจำ

การสร้างอินสแตนซ์ Lazy:

-(void)setThumbImageURL:(NSMutableArray *)thumbImageURL
{
    if (!_thumbImageURL) _thumbImageURL=[[NSMutableArray alloc] initWithCapacity:50];
    _thumbImageURL=thumbImageURL;
}

รหัสของฉัน:

[PXRequest requestForPhotoFeature:PXAPIHelperPhotoFeaturePopular resultsPerPage:50 page:1 photoSizes:(PXPhotoModelSizeLarge | PXPhotoModelSizeThumbnail | PXPhotoModelSizeSmallThumbnail |PXPhotoModelSizeExtraLarge) sortOrder:PXAPIHelperSortOrderCreatedAt completion:^(NSDictionary *results, NSError *error) {

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

        if (results) {
            self.photos =[results valueForKey:@"photos"];
            NSLog(@"%@",self.photos);
        }

        NSLog(@"\n\n\n\n\n\n\n\nSelf photos count  : %lu",[self.photos count]);
        for (int i=0; i<[self.photos count]; i++)
        {
            NSURL *thumbImageUrl= [NSURL URLWithString:[[[self.photos valueForKey:@"images"] [i] valueForKey:@"url"] firstObject]];
            NSData *imageData=[NSData dataWithContentsOfURL:thumbImageUrl];



            [self.thumbImageURL addObject:imageData];
            self.largeImageURL[i]=[[[self.photos valueForKey:@"images"] [i] valueForKey:@"url"] lastObject];

        }
        NSLog(@"\n\n\n\n\n\n\n\nSelf Thum Image after  : %@",self.thumbImageURL);
        NSLog(@"\n\n\n\n\n\n\n\nSelf large Image after  : %@n\n\n\n\n\n\n\n",self.largeImageURL);

    }];

person Jacob    schedule 31.05.2015    source แหล่งที่มา


คำตอบ (2)


ฉันเองก็พบปัญหา ปัญหาคือการสร้างอินสแตนซ์แบบขี้เกียจนั้นอยู่บนตัวตั้งค่าในขณะที่มันควรจะอยู่บนตัวทะเยอทะยาน

person Jacob    schedule 01.06.2015

เปลี่ยนอินสแตนซ์ Lazy:

จาก:

-(void)setThumbImageURL:(NSMutableArray *)thumbImageURL
{
    if (!_thumbImageURL) _thumbImageURL=[[NSMutableArray alloc] initWithCapacity:50];
    _thumbImageURL=thumbImageURL;
}

To:

@property (nonatomic, strong) NSMutableArray *thumbImageURL;
/**
 *  lazy load _thumbImageURL
 *
 *  @return NSMutableArray
 */
- (NSMutableArray *)thumbImageURL
{
    if (_thumbImageURL == nil) {
        _thumbImageURL = [[NSMutableArray alloc] initWithCapacity:50];
    }
    return _thumbImageURL;
}
person ElonChan    schedule 01.06.2015