簡體   English   中英

如何釋放NSUrlConnection類對象,NSMutableData類對象?

[英]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中,請告訴我它的發布位置。

  1. 您應該保存對NSURLConnection引用:

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

  2. 您應該啟動它:

    [connection start];

  3. 並且您應該在didFailWithErrorconnectionDidFinishLoading釋放它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM