18

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

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"]]]];

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];
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sam Baumgarten
  • 2,231
  • 3
  • 21
  • 46
  • 1
    Memory management has changed a lot in iOS5 with the introduction of Automated Reference Counting. You need to read a good introduction to ARC. I recommend [Ray Wenderlich's tutorial](http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1) by Matthijs Hollemans. – Thilo Dec 27 '12 at 00:41

2 Answers2

23

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"

Sam Baumgarten
  • 2,231
  • 3
  • 21
  • 46
21

Change

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

to

SBJSON *json4 = [SBJSON new];

This will allow you to leave automatic reference counting intact.

Jezen Thomas
  • 13,619
  • 6
  • 53
  • 91
  • Great Answer! I was avoiding the problem by turning off automatic reference counting. This actually shows how to fix the problem! – Sam Baumgarten Jan 28 '13 at 06:02