2012-01-03 1 views
10

나는 ARC로 놀기 시작했으며, 내가 시도한 첫 번째 실험 중 하나는 URL에 HTTP 호출을하고 데이터를 다시 얻는 것이었다. ARC와ARU로 NSURLConnection sendSynchronousRequest

NSError *error = [[NSError alloc] init]; 
NSHTTPURLResponse *responseCode = nil; 

NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:responseCode error:error]; 

내가 마지막 줄에 컴파일러 오류와 경고를 얻을 활성화 : 그 내가 좋아하는 sendSynchronousRequest을 사용하는 내 "고토"에 갔다 의미 때문에 물론, HTTP 상태 코드는 나에게 중요하다.

오류 :

Implicit conversion of an Objective-C pointer to 'NSURLResponse *__autoreleasing *' is disallowed with ARC

Implicit conversion of an Objective-C pointer to 'NSError *__autoreleasing *' is disallowed with ARC

file://localhost/Users/jason/Projects/test/Data/DataService.m: error: Automatic Reference Counting Issue: Implicit conversion of an Objective-C pointer to 'NSURLResponse *__autoreleasing *' is disallowed with ARC

file://localhost/Users/jason/Projects/test/Data/DataService.m: error: Automatic Reference Counting Issue: Implicit conversion of an Objective-C pointer to 'NSError *__autoreleasing *' is disallowed with ARC

경고 :

I는 기준 통과를 무엇을 말할 수에서

Incompatible pointer types sending 'NSHTTPURLResponse *_strong' to parameter of type 'NSURLResponse *_autoreleasing *'

Incompatible pointer types sending 'NSError *_strong' to parameter of type 'NSError *_autoreleasing *'

이를 망치는 무엇이지만, 내가 무엇을 올바른 방법으로 확실치 이 문제를 해결하십시오. ARC와 비슷한 작업을 수행하는 "더 나은"방법이 있습니까?

답변

22
NSError *error = nil; 
    NSHTTPURLResponse *responseCode = nil; 

    NSURLRequest *request; 

    NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error]; 

error/responceCode 포인터에 대한 참조가 누락되었습니다.

+0

LOL ... 공휴일 이후에 생각하려고하는 나를 도와줍니다! –

2

(NSHTTPURLResponse __autoreleasing *) 유형과 (NSError __autoreleasing *) 유형을 사용해야합니다.

NSHTTPURLResponse __autoreleasing *response = nil; 
NSError __autoreleasing *error = nil; 

// request 
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

그리고 당신은 다음과 그들을 처리 할 수 ​​

if (response){ 
    // code to handle with the response 
} 
if (error){ 
    // code to handle with the error 
} 

그렇지 않으면 수없는 글로벌 바르로 사용 응답 및 오류입니다. 그랬다면, 그들은 다음 correctly.Like 작동하지 않습니다 :

.h 
NSHTTPURLResponse *__autoreleasing *response; 
NSError *__autoreleasing *error; 

.m 
// request 
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:response error:error]; 

위의 코드는 작동하지 않습니다!