2010-02-15 73 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

相关问题