开发者

why shouldn't I release this string?

开发者 https://www.devze.com 2022-12-20 11:09 出处:网络
Look at the following method: -(void)updateProfile:(Profile *)profile WithJSON:(NSString *)JSON; { SBJSON *parser = [[SBJ开发者_如何学运维SON alloc] init];

Look at the following method:

-(void)updateProfile:(Profile *)profile WithJSON:(NSString *)JSON;
{
    SBJSON *parser = [[SBJ开发者_如何学运维SON 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];
}

I have commented out two lines, which gives me EXC_BAD_ACCESS if not.

Can someone explain to me why it's wrong to release these objects?


You shouldn't release it because you didn't +alloc, -retain, or -copy it. Convenience constructors like +objectWith… return autoreleased objects.


The better question to ask is: Why should you release it? What have you done to claim ownership over the object? The answer in this case is "nothing." Since you don't own it, you can't very well release it.

0

精彩评论

暂无评论...
验证码 换一张
取 消