我用下面的代碼來下載圖片:確定弱引用的對象是否已釋放
- (void)downloadImageAtUrl:(id)url
andDelegate:(id<IPServerDelegate>)delegate_ {
NSURL *correctUrl = nil;
if ([url isKindOfClass:[NSString class]])
correctUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kHostURL, [url substringFromIndex:1]]];
else
correctUrl = url;
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:correctUrl];
[imageRequestsArray addObject:request];
[request setCompletionBlock:^{
if (request.responseStatusCode == 200) {
if (delegate_ && [delegate_ respondsToSelector:@selector(didDownloadImage:atUrl:)]) {
[delegate_ didDownloadImage:request.responseData atUrl:request.url];
}
}
else {
if (delegate_ && [delegate_ respondsToSelector:@selector(failedToDownloadImageWithUrl:)]) {
[delegate_ failedToDownloadImageWithUrl:request.url];
}
}
[imageRequestsArray removeObject:request];
}];
[request setFailedBlock:^{
if (delegate_ && [delegate_ respondsToSelector:@selector(failedToDownloadImageWithUrl:)]) {
[delegate_ failedToDownloadImageWithUrl:request.url];
}
[imageRequestsArray removeObject:request];
}];
[request startAsynchronous];
}
如果delegate_對象已釋放應用程序崩潰。我如何確定對象delegate_已被釋放,而無需創建對它的直接引用?我知道iOS 5中的__weak指針,但我的應用必須與iOS 4.3兼容。
謝謝,但這不是一個解決方案。正如我所說的,我不能直接引用delegate_對象,它是答案中的「對象」。 – Eugene
Hummm,所以在任何時候你都可以用一個釋放對象結束? – Peres
是的,情況就是如此。 – Eugene