2011-06-15 30 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

相關問題