2010-02-15 71 views
2

請看下面的方法:爲什麼我不應該釋放這個字符串?

-(void)updateProfile:(Profile *)profile WithJSON:(NSString *)JSON; 
{ 
    SBJSON *parser = [[SBJSON alloc] init]; 
    NSDictionary *object = [parser objectWithString:JSON error:nil]; 

    NSNumberFormatter *nf = [[NSNumberFormatter alloc] init]; 
    [nf setPositiveFormat:@"#,##0"]; 

    profile.displayName = [object valueForKey:@"displayName"]; 
    profile.profileURL = [object valueForKey:@"profileURL"]; 

    NSString *rep = [object valueForKey:@"reputation"]; 
    profile.reputation = [[nf numberFromString:rep] intValue]; 
    //[rep release]; <-Why not release? 

    [nf release];   
    //[object release]; <-Why not release? 
    [parser release]; 
} 

我註釋掉兩行,這給了我EXC_BAD_ACCESS如果不是。
有人可以向我解釋爲什麼釋放這些對象是錯誤的嗎?

回答

14

您不應該發佈它,因爲您沒有+alloc,-retain-copy它。像+objectWith…這樣的便利構造函數返回自動釋放對象。

+0

謝謝!哇。這是顯而易見的......它解決了我認爲來自RegexKitLite框架的一些問題。 有一天,我會得到這個。我希望... – Vegar 2010-02-15 20:59:25

5

要問的更好的問題是:爲什麼應該你釋放它?你做了什麼來主張對象的所有權?這種情況下的答案是「沒有」。既然你不擁有它,你就不能很好地釋放它。

+1

猜猜我必須停止銷燬其他人的財產......謝謝。 – Vegar 2010-02-15 21:22:47

相關問題