【问题标题】:what kind of initialization is this called - conceptual?这称为什么样的初始化 - 概念性的?
【发布时间】:2010-07-12 19:20:54
【问题描述】:

我有这个来自苹果示例代码“LazyTableImages”的sn-p。在下面的代码中,他们正在初始化 IconDownloader 类。那么这是一种什么样的行为呢。

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

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

然后

    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];   
    }

objectForKey 文档这样说:

objectForKey:

返回与给定键关联的值。

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

所以我应该相信他们正在设置这条线

IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];

仅用于在对象中设置 nil 值。

最后的问题是上面的行是做什么的?

谢谢

【问题讨论】:

  • 不清楚您在问什么,而且格式更加混淆了您的问题。
  • @nicolai 现在还可以还是仍然模棱两可?

标签: iphone objective-c concept


【解决方案1】:

这一行:

IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];

没有制作新的 iconDonwloader。它只是询问 imageDownloadsInProgress 对象(我假设它是一个 NSDictionary?)尝试获取与键 'indexPath' 对应的 IconDownloader 对象 - 表中的当前行。

这段代码:

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];   
}

检查它是否存在。如果没有(imageDownloadsInProgress 返回 nil,即找不到该键的对象)创建一个新对象并将其添加到 imageDownloadsInProgress NSDictionary。

所有这些代码意味着对于每个 indexPath(表格中的每一行)只有一个 IconDownloader 对象 - 当您上下滚动表格时,它会阻止您多次尝试下载图标。

希望对您有所帮助。

【讨论】:

  • 换句话说,他们使用 NSDictionary 作为图像缓存。这是一种常见的模式,您会在很多应用程序中看到它。
【解决方案2】:

imageDownloadsInProgress 似乎是一个 NSMutableDictionary。该字典用于保存类 IconDownloader 的实例。实例存储在相应的indexPath下,因此很容易获取tableView中给定行的IconDownloader。

你问的那行就是这样做的。如果 IconDownloader 之前没有被实例化并存储在字典中,它会为给定的 indexPath 或 nil 检索 IconDownloader 实例。

【讨论】:

    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多