【问题标题】:error 'autorelease' is unavailable: not available in automatic reference counting mode错误“自动释放”不可用:在自动引用计数模式下不可用
【发布时间】:2011-09-15 20:29:54
【问题描述】:

我尝试使用 Stig 的 JSON 库发出 HTTP 请求并解析 JSON。我收到此错误“自动释放”不可用:当我使用此代码时,在自动引用计数模式下不可用

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:@"http://sandbox.CompanyName.com/api/%@/users/%@/user_badges?url=CompanyName.map2.com&amount=999999999999",[信息 stringForKey:@"apiKey"] , [信息 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];

【问题讨论】:

  • 随着自动引用计数的引入,iOS5 中的内存管理发生了很大变化。您需要阅读 ARC 的良好介绍。我推荐 Ray Wenderlich's tutorial by Matthijs Hollemans。

标签: iphone ios automatic-ref-counting


【解决方案1】:

消除此错误的方法是进入您的项目构建设置。搜索自动引用计数。找到后将值设置为“NO”

【讨论】:

    【解决方案2】:

    改变

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

    SBJSON *json4 = [SBJSON new];

    这将允许您保持自动引用计数不变。

    【讨论】:

    • 很好的答案!我通过关闭自动引用计数来避免这个问题。这实际上显示了如何解决问题!
    猜你喜欢
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多