ios 错误“自动释放”不可用:在自动引用计数模式下不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6363566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:22:57  来源:igfitidea点击:

error 'autorelease' is unavailable: not available in automatic reference counting mode

iphoneiosautomatic-ref-counting

提问by Sam Baumgarten

I trying to make a HTTP request and parse JSON using Stig's JSON Library. I'm getting this error 'autorelease' is unavailable: not available in automatic reference counting mode when I use this code

我尝试使用 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]; 


UPDATE

更新

For anyone interested, this is the correct code: 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"]]]];

对于任何感兴趣的人,这是正确的代码: 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];

回答by Sam Baumgarten

The way that you get rid of this error is to go into your projects build settings. Search for automatic reference counting. Once you find it set the value to "NO"

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

回答by Jezen Thomas

Change

改变

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

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

to

SBJSON *json4 = [SBJSON new];

SBJSON *json4 = [SBJSON new];

This will allow you to leave automatic reference counting intact.

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