0

我有一个让我发疯的错误。我有一个NSArray被叫的问题。该数组由 JSON 响应填充。我正在尝试使用它来填充表格视图。

在我的头文件中,我正在定义这样的问题

@interface OneViewController : UITableViewController <MBProgressHUDDelegate> {
    NSArray *questions;
    MBProgressHUD *HUD;
}

@property (nonatomic, strong) NSArray *questions;

在我的实现中,我像这样实例化它

@synthesize questions;

当视图加载时,我正在调用这个函数来 ping 我的 JSON 服务

- (void) pullQuestions {
    NSMutableDictionary *viewParams = [NSMutableDictionary new];
    [viewParams setValue:@"questions" forKey:@"view"];
    [PythonView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
        questions = responseObject;
        NSLog(@"Qestions: %@", responseObject);
        [self.tableView reloadData];
        [HUD hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //ALog(@"Failure: %@", [error localizedDescription]);
    }];
}

当我得到响应时,我会追踪响应对象,它是一个格式正确的 JSON 响应。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"VideoFeedCell";
        NSInteger count = [questions count];
        NSLog(@"Count: %i", count);
        for (int i = 0; i < count; i++) {
            NSString* body = [questions objectAtIndex:i];
            NSLog(@"Object: %@", body);
        }
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"VideoFeedCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];   
    }
    return cell;
}

因此,当我遍历此内容时,问题计数为 4,并且跟踪了数组中的第一个对象(为 null)。它第二次尝试执行 for 循环时,我得到EXC_BAD_ACCESS. 几乎就像问题数组消失了一样。最奇怪的是我在另一个项目中使用了几乎完全相同的代码,它工作正常。我什至使用我在另一个项目中使用的相同 JSON 服务对其进行了测试(所以我知道它没有格式错误或其他什么),但我仍然收到此错误。我真的卡住了,我觉得它必须与保存有关JSON响应作为一个数组或它自己被释放的数组的一些奇怪的东西。

4

2 回答 2

0

问题与 ARC 有关。我从一些旧的样板代码开始(我不久前建立的一个选项卡式导航项目)。并试图对其进行改造。我没有注意到一个项目与 ARC 交战,而一个项目没有。

于 2013-01-31T15:18:16.683 回答
0

假设您没有使用 ARC,我怀疑问题在于您将responseObject直接分配给questions实例变量而不是通过属性。这意味着您没有获得所有权,responseObject并且在刷新自动释放池时它会被释放。

尝试改变:

questions = responseObject;

至:

questions = [responseObject retain];

或者:

self.questions = responseObject;

看看是否能解决问题。

于 2013-01-31T15:16:02.807 回答