0

This is my code trying to create NSMutable array with strings and then to store them in the object property. NSArray *photos works but not the NSMUtableArray thumbImageURL. When i NSLog it for debugging purposes it is null. Please help, it annoys me a lot, cant find a solution. i also lazy instantiated so there is no reason it will not have allocation in the memory.

Lazy Instantiation:

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

My code:

[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);

    }];
donfuxx
  • 11,277
  • 6
  • 44
  • 76
Jacob
  • 1
  • 1

2 Answers2

0

I myself found the problem. The problem is that lazy instantiation is on the setter whereas it should be on the getter

Jacob
  • 1
  • 1
0

Change the Lazy Instantiation:

From:

-(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;
}
ChenYilong
  • 8,543
  • 9
  • 56
  • 84