2011-08-31 49 views
0
NSError *theError = nil; 
NSArray *keys = [NSArray arrayWithObjects:@"password", @"userId", nil]; 
NSArray *objects = [NSArray arrayWithObjects:passwordTextField.text, userNameTextField.text, nil]; 
NSDictionary *requestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

NSString *JSONString =[requestDictionary JSONRepresentation]; 
NSData *JSONData =[JSONString dataUsingEncoding:NSUTF8StringEncoding]; 
NSLog(@"JSONString :%@", JSONString); 
NSLog(@"JSONData :%@", JSONData); 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://153.20.32.74/11AprP306/passenger/jsonitem"]]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:JSONData]; 
[request setHTTPMethod:@"POST"]; 

NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError]; 
NSLog(@"response : %@", theResponse); 
NSLog(@"error : %@", theError); 
NSLog(@"data : %@", data); 
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
NSLog(@"string: %@", string); 
[string release]; 
//[theResponse release]; // this statement crashes the app 

有它得到的东西与做这样的说法:NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError]; 我看到使用的&象征。这是什么意思?为什么发布声明在这里崩溃了应用程序?

回答

1

好了,你自动释放theResponse当实例,所以释放它两次是造成你的问题。要么不要拨打autorelease电话,要么不要拨打release电话。

个人而言,我摆脱autorelease的。 release为您的程序运行提供了更好的控制。

哦,&没有什么可担心的 - 它只是通过它所进行变量的地址。在这种情况下,您需要通过NSURLResponse**。由于您有NSURLResponse*,因此您将参考文献传递给它。

+0

对不起,自动释放陈述并没有想在那里。我加入来解决问题。 – John

1

这是因为theResponse已发送的消息autorelease

NSURLResponse *theResponse =[[[NSURLResponse alloc]init] autorelease]; 

如果releaseautoreleased您会导致应用程序崩溃的垃圾收集器会在释放对象的对象。

&只是意味着“给我的theErrortheResponse(基本上要传递指针的一个指针,它是由该方法sendSynchronousRequest:returningResponse:error:必需)

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request 
       returningResponse:(NSURLResponse **)response 
          error:(NSError **)error 

NSURLResponse **NSError **手段“地址的地址的地址'所以给他们只有theErrortheResponse(没有&)只会在预期的时候给他们的方法'他们的地址'

+0

对不起,autorelease声明不假设在那里。我加入来解决问题 – John

+0

我只是想知道为什么如果我手动调用release方法,它会崩溃。 – John

+0

好吧我明白,你做错了我会发布正确的答案,但你应该提到你已经编辑了你的问题本身的问题,因为现在,有人进入你的问题不会理解我们以前的答案 – apouche

3

I'l l当你编辑你的问题时,将其作为新的答案发布。

你这样做是错误的。

这是sendSynchronousRequest:returningResponse:error:的责任来为您创建的响应(或错误,如果出事了)

这就是为什么你需要传递一个指针theResponse和指向theError

当拨打sendSynchronousRequest:returningResponse:error:已完成,theResponse将被创建,最重要的是autoreleased(通过sendSynchronousRequest:returningResponse:error)!

所以最后你回到了autorelease/over release问题。

正确的代码是:

NSURLResponse *theResponse = nil; // no need to init it will be done later on 
NSError *theError = nil; // no need to init either 
NSData *data = [NSURLConnection sendSynchronousRequest:request 
            returningResponse:&theResponse 
               error:&theError]; 

if (aError != nil) { } // handle error 
else {} // handle your response data 

//no need to release theResponse