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

最佳答案

对于我来说,- (void) downloadComplete: (id) downloadedContent是一个委托方法。这是一个好习惯,即只释放您拥有的对象(除非有明确说明),而委托则不拥有该对象。用deallocViewController.m方法释放对象要好得多,也更清楚

10-08 06:27