Ios 使用AFN网络中的JSON解析数据时,UITableView崩溃

Ios 使用AFN网络中的JSON解析数据时,UITableView崩溃,ios,uitableview,afnetworking,jsonkit,Ios,Uitableview,Afnetworking,Jsonkit,我目前正在尝试解析web服务中的数据,并在UITableView中对其进行格式化 项目详情: 部署目标:ios 4.3 jsonTapped函数: -(void) jsonTapped { AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost"]]; NSMutableURLRequest *request = [http

我目前正在尝试解析web服务中的数据,并在UITableView中对其进行格式化

项目详情: 部署目标:ios 4.3

jsonTapped函数:

-(void) jsonTapped
{
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL   URLWithString:@"http://localhost"]];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                        path:@""
                    parameters:@{@"provider":@"12",
                                @"var":@"titles"}];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
{
    // Print the response body in text
    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
    NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    orderItems = [str objectFromJSONString];
    list = [orderItems objectAtIndex:0];
} 
failure:^(AFHTTPRequestOperation *operation, NSError *error) 
{
    NSLog(@"Error: %@", error);
}];
[operation start];
}
数据以字典数组的形式出现。我已经用类检查函数进行了检查。 List是在.h文件中声明的字典,orderItems是在.h文件中声明的NSArray

当我去更新我的UITableView时,事情变得非常糟糕。如果我试图访问列表中的一段数据(在我的jsonTapped函数之外的任何地方),整个过程都会崩溃

我怎样才能解决这个问题?关于为什么我输入到orderItems数组中的信息没有被保留,有什么想法吗

当它崩溃时,它只会说(lldb),然后指示我编写一个绿色错误的代码,上面写着“thread1:EXC_BAD_ACCESS…”


谢谢

我看到您正在为类变量分配一个自动释放的对象。如果您正在使用ivar,您必须自己保留这些信息。使用可以声明为retain的属性更容易。以下是方法:

在你的

@interface MyClass {

    NSArray *orderItems;
    NSDictionary *list;
}

@end
变成:

@interface MyClass

@property (nonatomic, retain) NSArray *orderItems;
@property (nonatomic, retain) NSDictionary *list;

@end
在.m文件中,在
jsonTapped
方法中:

orderItems = [str objectFromJSONString];
list = [orderItems objectAtIndex:0];
变成:

self.orderItems = [str objectFromJSONString];
self.list = [orderedItems objectAtIndex:0];
您也可以这样做:

orderItems = [[str objectFromJSONString] retain];
list = [[orderItems objectAtIndex:0] retain];
但是,你必须记住在某个地方释放它们,否则你会有内存泄漏

对于属性,只需将其设置为
nil
。编译器将知道在必要时释放对象

以下是您的
dealloc
方法的外观:

- (void)dealloc {

    self.orderItems = nil;
    self.list = nil;

    ...

    [super dealloc];
}

你能公布你的车祸详情吗?