2011-06-15 28 views
13

我试图使用Stig的JSON库进行HTTP请求和解析JSON。我得到这个错误“自动释放”不可用:自动引用计数模式不可用时,我使用此代码错误'autorelease'不可用:在自动引用计数模式下不可用

NSURLRequest *request2; 
request2 = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://sandbox.CompanyName.com/api/%@/users/%@/user_badges?url=CompanyName.map2.com&amount=999999999999",[information stringForKey:@"apiKey"] , [information stringForKey:@"userID"]]]]; 

NSURLConnection *connection2; 
connection2 = [[NSURLConnection alloc] initWithRequest:request2 delegate:self startImmediately:YES]; 
NSURLResponse *resp2; 
NSData *cData2 = [NSURLConnection sendSynchronousRequest:request2 returningResponse:&resp2 error:nil]; 
NSString *cDataString2 = [[NSString alloc] initWithData:cData2 encoding:NSUTF8StringEncoding]; 
NSLog(@"getUsersBadges called"); 
NSError *error4; 
SBJSON *json4 = [[SBJSON new] autorelease]; 
// NSArray *luckyNumbers = [json objectWithString:responseString error:&error]; 
NSDictionary *luckyNumbers4 = [json4 objectWithString:cDataString2 error:&error4]; 

[cDataString2 release]; 

UPDATE

任何有兴趣,这是正确的代码: NSURLRequest * request2; request2 = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@“http://sandbox.CompanyName.com/api/%@/users/%@/user_badges?url=CompanyName.map2.com&amount=999999999999”,[information stringForKey:@“apiKey”],[information stringForKey:@“userID”]]]];

NSURLConnection *connection2; 
connection2 = [[NSURLConnection alloc] initWithRequest:request2 delegate:self startImmediately:YES]; 
NSURLResponse *resp2; 
NSData *cData2 = [NSURLConnection sendSynchronousRequest:request2 returningResponse:&resp2 error:nil]; 
NSString *cDataString2 = [[NSString alloc] initWithData:cData2 encoding:NSUTF8StringEncoding]; 
NSLog(@"getUsersBadges called"); 
NSError *error4; 
SBJSON *json4 = [SBJSON new]; 
// NSArray *luckyNumbers = [json objectWithString:responseString error:&error]; 
NSDictionary *luckyNumbers4 = [json4 objectWithString:cDataString2 error:&error4]; 
+1

随着引入自动引用计数,内存管理在iOS5中发生了很大变化。你需要阅读一篇关于ARC的好介绍。我推荐Matthijs Hollemans撰写的[Ray Wenderlich的教程](http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1)。 – Thilo 2012-12-27 00:41:15

回答

18

变化

SBJSON *json4 = [[SBJSON new] autorelease];

SBJSON *json4 = [SBJSON new];

这将允许你离开自动引用计数完好。

+0

伟大的答案!我通过关闭自动引用计数来避免这个问题。这实际上显示了如何解决问题! – 2013-01-28 06:02:00

23

您摆脱此错误的方式是进入您的项目构建设置。搜索自动引用计数。一旦你找到它的值设置为 “否”

+0

+1你救了我的命,谢谢你...! – Dinesh 2012-06-01 05:55:19

+0

+1谢谢你,它为我工作了很多 – Babul 2012-10-23 15:01:09

相关问题