2

この警告を回避しようとしています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. 

コード全体は正常に動作しますが、予防策を講じているだけです。

4

2 に答える 2

4

どちらのアプローチもオブジェクトを割り当てます。

アロケートで、

[[NSURLConnection alloc]initWithRequest:request delegate:self];

あなたはそれを解放する責任があります。

connectWithRequest では、

[NSURLConnection connectionWithRequest:request delegate:self];

自動解放プールによって保持されます。私の推測では、自動解放プールによって保持されるため、それを解放するためのハンドルは必要なく、コンパイラーは自動解放プールがハンドルを持っていることに満足しています。

alloc を使用すると、コンパイラはおそらく、後で解放するためにハンドルを保持することを期待しています。したがって、これは警告としてフラグが立てられます。

実際には、ハンドルを明示的に保持するかどうかにかかわらず、デリゲート メソッドがハンドルを取得します。そのため、デリゲート メソッドに渡されたハンドルを使用して解放できます。その場合、警告は本当に偽物ですが、単なる警告です。

于 2012-05-10T07:13:09.943 に答える
1

次のことができます。

[NSURLConnection connectionWithRequest:request delegate:self];

それ以外の:

[[NSURLConnection alloc]initWithRequest:request delegate:self];
于 2012-05-10T06:32:26.447 に答える