正在尝试避免此警告,正在使用NSURLConnection
:
-(void)goGetData{
responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://somefile.php"]];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
}
- (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 {
//label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
NSLog(@"Connection failed: %@",error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSMutableArray *qnBlock = [responseString JSONValue];
for (int i = 0; i < [qnBlock count]; i++){
NSLog(@"%@",[qnBlock objectAtIndex:i]);
}
}
警告位于以下行:
[[NSURLConnection alloc]initWithRequest:request delegate:self];
警告是:
Expression result unused.
整个代码运行良好,但我只是采取预防措施。
最佳答案
两种方法都分配一个对象。
通过分配,
[[NSURLConnection alloc]initWithRequest:request delegate:self];
您负责发布它。
使用 connectWithRequest,
[NSURLConnection connectionWithRequest:request delegate:self];
它由自动释放池保留。我的猜测是,由于它由自动释放池保留,因此您不需要句柄来释放它,并且编译器会满意自动释放池具有该句柄。
使用alloc,编译器可能希望您保留一个句柄以便稍后释放。因此它将其标记为警告。
事实上,委托(delegate)方法会获取句柄,无论您是否显式保留句柄。因此,您可以通过传递给委托(delegate)方法的句柄来释放它。那么这个警告确实是虚假的,但它只是一个警告。
关于ios - 使用异步 NSURLConnection 获取警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10528546/