当我在completion中创建NSArray时,它是空的,而第一个NSArray不是空的



这是我的代码试图用字符串创建NSMutable数组,然后将它们存储在对象属性中。NSArray *photos工作,但不是NSMUtableArray thumbImageURL。当我为调试目的而NSLog它时,它是空的。请帮忙,这让我很烦恼,找不到解决办法。我还延迟实例化,所以没有理由它不会在内存中分配。

延迟实例化:

-(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(@"nnnnnnnnSelf 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(@"nnnnnnnnSelf Thum Image after  : %@",self.thumbImageURL);
        NSLog(@"nnnnnnnnSelf large Image after  : %@nnnnnnnn",self.largeImageURL);
    }];

我自己发现了这个问题。问题是延迟实例化在setter上而它应该在getter上

更改惰性实例化:

来自:

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

:

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

最新更新