我正在尝试使用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.
整个代码运行良好,但我只是采取预防措施。
答案 0 :(得分:4)
两种方法都分配一个对象。
使用alloc,
[[NSURLConnection alloc]initWithRequest:request delegate:self];
你有责任释放它。
使用connectWithRequest,
[NSURLConnection connectionWithRequest:request delegate:self];
它由自动释放池保留。我的猜测是,因为它是由自动释放池保留的,所以你不需要一个句柄来释放它,并且编译器认为自动释放池有句柄。
使用alloc,编译器可能希望您保留稍后释放的句柄。因此它将此标记为警告。
事实是委托方法获取句柄,无论你是否明确地保留一个句柄。因此,您可以使用传递给委托方法的句柄释放它。这个警告真的很虚假,但它只是一个警告。
答案 1 :(得分:1)
您可以执行以下操作:
[NSURLConnection connectionWithRequest:request delegate:self];
而不是:
[[NSURLConnection alloc]initWithRequest:request delegate:self];