// ViewController.m - implements downloader's protocol downloadComplete
- (void) startDownload
{
Downloader *downloader = [[downloader alloc] init];
[downloader setDelegate:self];
[downloader startDownloading];
// [downloader release] or autorelase makes the program crash
}
- (void) downloadComplete: (id) downloadedContent
{
[downloadedContent release]; // will this release the object allocated in the first function? Or do I set [self release] in dealloc of Downloader.m? Or any other way to do it?
}
// Downloader.m
- (void) startDownloading
{
// download some data
[[self delegate] downloadComplete:self];
}
答案 0 :(得分:1)
实际上它不是很可可。我建议创建downloader
作为实例方法,在需要时分配它并在dealloc中释放它。如果您可以在同一控制器实例中多次使用下载功能 - 请确保在创建下载器对象之前将其释放并清零。
答案 1 :(得分:1)
对于我所看到的,- (void) downloadComplete: (id) downloadedContent
是委托方法。只释放你拥有的对象(除非明确说明),并且委托不拥有该对象,这是一个好习惯。使用dealloc
ViewController.m
方法释放对象要好得多,也更清晰