[英]How to release the NSUrlConnection class object,NSMutableData class object?
iam進行一個應用程序。在該iam中,使用NSUrlconnection類。下面是我的代碼。
- (void)viewDidLoad {
[super viewDidLoad];
responsedata = [[NSMutableData data] retain];
NSString *url = [NSString stringWithFormat:@"https://www.google.com"];
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:URL];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[request release];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responsedata setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responsedata appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
}
在此代碼中,當執行iam時,它表示在responsedata = [[NSMutableData data] retain];
內存泄漏responsedata = [[NSMutableData data] retain];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
在viewDidLoad()
SO中,請告訴我它的發布位置。
您應該保存對NSURLConnection
引用:
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
您應該啟動它:
[connection start];
並且您應該在didFailWithError
或connectionDidFinishLoading
釋放它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.