在 objective C 中通过 prepareForSegue 存储核心数据数组

Storing coredata array through prepareForSegue in objective C

我需要将一组 CoreData 实体从我的主视图传递到 TableViewController。

我使用了很多 Whosebug 帖子来帮助自己解决这个问题,并且认为它有效。

但是当我构建我的应用程序时,我遇到了线程问题,显示:

[NSTaggedPointerString count]: unrecognized selector sent to instance".

我做了一些检查,问题是,在我的 tableView 中,行数设置为 0。

所以这是我的代码:

我的 prepareForSegue 函数在我的 MainViewController.m:

(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"sgPushDetail"])
    {
        checkDataSavedTableViewController *detail = segue.destinationViewController;
        detail.datasToDisplay = _listOfDatas;
    }
}

这是我将数据放入 TableViewController 的函数:

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dataCells" forIndexPath:indexPath];
    NSString *rowArray = self.datasToDisplay[indexPath.row];
    cell.textLabel.text = rowArray;
    return cell;
}

(但是错误出现在ViewDidLoad中所以meeeh)

我该如何继续?

prepareforsegue中放置断点。并检查你的数组。我认为您的数组为 nil,因此您的 table 视图未获取数据。放置必要的断点,或者您可以在 prepareforsegueviewdidload 中打印数组 tableviewcontroller 并检查您是否正在获取数组中的数据!

希望这会有所帮助:)

你的错误在NSString *rowArray = self.datasToDisplay[indexPath.row];

datasToDisplay 是一个 NsManagedObject 数组,而不是一个字符串数组。所以你需要从你的 NsManagedObject 中获取字符串值:

NSString *rowArray = [self.datasToDisplay[indexPath.row] valueForKey: @"stringKey"]

好吧,我很笨。

因为我使用了 Core Data,所以我已经有了我的实体列表。我不需要随机 Meh 的 NSArray,我只需要:

NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context];