Iphone 错误';自动释放&x27;不可用:在自动引用计数模式下不可用
我试图使用Stig的JSON库发出HTTP请求并解析JSON。我得到一个错误“自动释放”不可用:当我使用此代码时,在自动引用计数模式下不可用Iphone 错误';自动释放&x27;不可用:在自动引用计数模式下不可用,iphone,ios,automatic-ref-counting,Iphone,Ios,Automatic Ref Counting,我试图使用Stig的JSON库发出HTTP请求并解析JSON。我得到一个错误“自动释放”不可用:当我使用此代码时,在自动引用计数模式下不可用 NSURLRequest *request2; request2 = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://sandbox.CompanyName.com/api/%@/users/%@/user_badges?url=Co
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];
更新 对于任何感兴趣的人,这是正确的代码: NSURLRequest*request2; request2=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@],[information stringForKey:@“apiKey”],[information stringForKey:@“userID”]]
消除此错误的方法是进入项目生成设置。搜索自动参考计数。找到后,将值设置为“否”更改
SBJSON*json4=[[SBJSON新建]自动释放]代码>
到
SBJSON*json4=[SBJSON new]代码>
这将允许您保持自动引用计数不变。随着自动引用计数的引入,iOS5中的内存管理发生了很大变化。你需要读一本很好的ARC简介。我推荐Matthijs Hollemans。答案很好!我通过关闭自动参考计数来避免这个问题。这实际上说明了如何解决问题!
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];