0

我正在用字符串对象填充数组。可能有数千个。我在 for 循环中做了很多 alloc 和 inits,我知道这很昂贵。有什么更好、更有效的方法来做到这一点?

谢谢

 //loop through the array of dictionaries
 for(NSUInteger i =0; i < [latestResults count]; i++){

        self.workingEntry = [[AppRecord alloc] init];

        //Get the next dictionary from the array of dictionaries
        NSDictionary *currentRecord = [latestResults objectAtIndex:i];


        //Set the object
        [self.workingEntry setAppURLString:[currentRecord valueForKeyPath:@"id.label"]];
        [self.workingEntry setAppName:[currentRecord valueForKeyPath:@"im:name.label"]];

        NSArray *imageArray = [currentRecord valueForKeyPath:@"im:image.label"];

        [self.workingEntry setImageURLString:[imageArray objectAtIndex:0]];
        [self.workingEntry setArtist:[currentRecord valueForKeyPath:@"im:artist.label"]];

       //Add object to array
       [self.workingArray addObject:self.workingEntry];

        currentRecord = nil;
        self.workingEntry = nil;
        imageArray = nil;


    }
4

1 回答 1

2

Off the cuff, I'd be more worried about all those valueForKeyPath: calls than calls to +alloc.

Irrelevant until it is measured, though.

If you want to reduce the # of allocations, move to using singletons or a global cache or some other mechanism to uniquify the objects. Of course, you then run the risk of a performance bottleneck in cache maintenance.

于 2012-09-14T16:12:10.617 回答