views:

39

answers:

2

Hi all,

I have this snippet from apple sample code "LazyTableImages" . In the code below they are initializing the IconDownloader class . So what kind of behavior is this .

*************************This Line ******************************************
    IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath]; 

**************************************************************************

and then

    if (iconDownloader == nil) 
    {
        iconDownloader = [[IconDownloader alloc] init];
        iconDownloader.CustomObject = CustomObject;
        iconDownloader.indexPathInTableView = indexPath;
        iconDownloader.delegate = self;
        [imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];
        [iconDownloader startDownload];
        [iconDownloader release];   
    }

and the objectForKey docs says this :

objectForKey:

Returns the value associated with a given key.

- (id)objectForKey:(id)aKey
Parameters

aKey

    The key for which to return the corresponding value.

Return Value

The value associated with aKey, or nil if no value is associated with aKey.
Availability

    * Available in iPhone OS 2.0 and later.

So should I believe they are setting this line

IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];

just for setting the nil value in the object .

ultimately the question is what does the above line do ?

thanks

+3  A: 

Hi,

This line :

IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];

isn't making a new iconDonwloader. It's just asking the imageDownloadsInProgress object (which I assume is a NSDictionary?) to try to get the IconDownloader object that coprresponds to the key 'indexPath' - the current row in the table.

This bit of code :

if (iconDownloader == nil) 
{
    iconDownloader = [[IconDownloader alloc] init];
    iconDownloader.CustomObject = CustomObject;
    iconDownloader.indexPathInTableView = indexPath;
    iconDownloader.delegate = self;
    [imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];
    [iconDownloader startDownload];
    [iconDownloader release];   
}

checks to see if it exists. If it doesn't (the imageDownloadsInProgress returned nil i.e. it can't find an object for that key) make a new one and add it to the imageDownloadsInProgress NSDictionary.

All of this code this means that for each indexPath (each row in the table) there is only ever one IconDownloader object - it stopd you trying to download the icon more than once as you scroll the table up and down.

Hope that helps.

deanWombourne
Ok thanks . I understand
hib
In other words, they're using the NSDictionary as a cache of images. This is a common pattern that you'll see in more than a few applications.
Brad Larson
@Brad Larson Thanks sir
hib
+1  A: 

imageDownloadsInProgress seems to be a NSMutableDictionary. This dictionary used used to hold instances of class IconDownloader. The instances are stored under the corresponding indexPath, so it is easy to get the IconDownloader for a given row in the tableView.

The line you ask about just does this. It retrieves an IconDownloader instance for a given indexPath or nil, if an IconDownloader has not been instantiated and stored in the dictionary before.

tonklon