使用stringWithFormat时出现EXC_BAD_ACCESS错误

时间:2012-09-10 18:56:37

标签: objective-c ios xcode exc-bad-access

这是我的标题部分:

@interface RootViewController : UIViewController
{
    NSString *status_id;
}

在控制器文件中,我正在分配变量:

- (void)updateStatus
{
    NSURL *url = [NSURL URLWithString:@"http://localhost/RightNow/API/status.json"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    NSError *error = [request error];
    NSString *response = [NSString alloc];

    NSError *error2;
    NSData* data = [response dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data      options:kNilOptions error:&error2];
    status_id = [json objectForKey:@"id"];
}

现在,当我再次尝试使用status_id时,我收到错误

- (IBAction)likeClick:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"http://localhost/RightNow/API/vote"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setRequestMethod:@"POST"];
    [request setPostValue:status_id forKey:@"id"]; //The error comes here
    [request setPostValue:@"like" forKey:@"vote"];
    [request startSynchronous];
}
抱歉我的英语不好。 请帮帮我,谢谢!

1 个答案:

答案 0 :(得分:1)

[json objectForKey:@“id”];将返回autorelease池中的对象。您需要向其发送复制消息,如

status_id = [[json objectForKey:@"id"] copy];

并在适当时释放它(如果不使用ARC)