ios - NSArray 返回 NULL?

标签 ios objective-c

我遇到了一个奇怪的问题。我正在尝试将数组 (storageData) 设置为等于从端点 (responseObject) 发回的数据。我已经记录了 responseObject,并且数据在那里,但是出于某种原因,当我尝试将其设置为 storageData 时,storageData 返回 NULL(即使我已经声明 storageData 在我的 block 之外使用)。有谁知道为什么会这样?见代码:

.h

@property (strong, nonatomic) NSArray *storageData;

.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

[DIOSNode nodeIndexWithPage:@"0" fields:@"title" parameters:[NSArray arrayWithObjects:@"storage_item", nil] pageSize:@"20" success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Nodes retrieved!");

    self.storageData = responseObject;

    [self.tableView reloadData];
    NSLog(@"%@",storageData);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure
}];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *DoctorsTableIdentifier = @"StorageItemTableViewCell";

    StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorageItemTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

       NSDictionary *temp = [storageData objectAtIndex:indexPath.row];
       NSString *title = [temp objectForKey:@"title"];
      [[cell itemName] setText:title];

    //  NSLog(@"%@", storageData);

    }
    return cell;
}

最佳答案

nodeIndexWithPage:... 可能是异步执行的。

它在获取数据之前返回,然后您的 TableView 方法被硬连线到 5 行,因此,该表会尝试填充尚未加载的数据。

让您的 numberOfRowsInSection: 方法返回 [storageData count]

您已经在完成 block 中调用了 -reloadData,因此该表将在成功加载时自动刷新。


您是否还手动声明实例变量?

如果是这样,那么 storageData = ...self.storageData = 实际上不会设置相同的东西(除非你也覆盖 -storageData 和 -setStorageData: ).

通过 self.storageData 使所有内容都引用它。


您确定您只使用显示数据的类的一个实例吗?

即将 NSLog(@"%p", self); 添加到 viewDidLoad 的开头。希望它只记录一次。它应该只打印一个十六进制数。

NSLog() 总是打印一些东西。如果它没有打印任何东西,那么代码就没有被执行。

关于ios - NSArray 返回 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29976093/

相关文章:

ios - 无法在沙箱外部为 THEOS Tweak 创建目录

ios - uitapgesturerecognizer 不适用于 iOS subview

objective-c - NSSoundDelegate 未被调用

objective-c - objc_msgSend 15 崩溃

objective-c - 获取 UIScrollView 内容的可见矩形

objective-c - 在存在ARC的情况下初始化iVar变量的正确方法是什么

ios - 获取 UIBarButtonItem 的框架

objective-c - 使用 CGAffineTransform 的奇怪结果

ios - 我可以取消 UILongPressGestureRecognizer,关闭 View 并长按以识别下面的 View 吗?

ios - 应用程序使用 writeToFile 在 iPhone 上保存的数据是否可以包含在 Spotlight 搜索的结果中?