NSArray adalah nol ketika saya membuatnya dalam penyelesaian sedangkan nsarray pertama bukan nol

Ini adalah kode saya yang mencoba membuat array NSMutable dengan string dan kemudian menyimpannya di properti objek. NSArray *photos berfungsi tetapi NSMUtableArray thumbImageURL tidak berfungsi. Ketika saya NSLog untuk tujuan debugging, nilainya nol. Tolong bantu, ini sangat mengganggu saya, tidak dapat menemukan solusi. saya juga malas membuat instance jadi tidak ada alasan tidak ada alokasi di memori.

Instansiasi Malas:

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

Kode saya:

[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 sumber


Jawaban (2)


Saya sendiri yang menemukan masalahnya. Masalahnya adalah instantiasi yang malas ada di setter padahal seharusnya ada di pengambil

person Jacob    schedule 01.06.2015

Ubah Instansiasi Malas:

Dari:

-(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