ios - 迭代传递给 View 的核心数据

标签 ios xcode core-data nsmanagedobjectcontext viewcontroller

我正在使用此方法将数据传递到 View :

nextViewController = [[AfricanSwallowViewController alloc] initWithNibName:@"AfricanSwallowView" bundle:nil];
((InstructionsViewController *)nextViewController).byTheHusk = byTheHusk;

我正在尝试迭代 byTheHusk 传递到的 View 中的值。

我已经尝试了多个版本的 NSLog(@"%@", byTheHusk.name); 并使用了 objectAtIndexPath 等,但没有成功

有没有办法在 whilefor 循环样式结构中访问对象条目属性?

我能得到的最接近的是:

NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:0];
NSLog(@"%@",[[managedObject valueForKey:@"name"] description]);

结果是:

ByTheHusk[7880:207](空)

这意味着有 207 个条目。是对的吗? ByTheHusk 是否正确通过?

我对这一切一窍不通,所以感谢您的帮助!!!

最佳答案

我认为您将 NSManagedObjectContext 对象与 NSManagedObject 对象混淆了。

此代码段:

nextViewController = [[AfricanSwallowViewController alloc] initWithNibName:@"AfricanSwallowView" bundle:nil];
((InstructionsViewController *)nextViewController).byTheHusk = byTheHusk;

... 看起来像是将 NSManagedObjectContext 从一个 View Controller 传递到另一个 View Controller 的尝试。但是,尝试的 Actor 阵容可能会把事情搞砸。它应该看起来像这样:

InstructionsViewController *nextViewController = [[AfricanSwallowViewController alloc] initWithNibName:@"AfricanSwallowView" bundle:nil];
nextViewController.byTheHusk = self.byTheHusk; //assuming that byTheHusk is a property of the current view controller. 

我猜测 InstructionsViewController 是一个使用 NSFetchedResultsController 的 TableView Controller ,并配置为从 byTheHusk 托管对象上下文中获取。

如果是这样,那么从这段代码中得到一个 null 返回值:

NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:0];
NSLog(@"%@",[[managedObject valueForKey:@"name"] description]);

... 可能表示 fetchedResultsController 在获取时找不到任何对象。在任何情况下,它都不会记录 byTheHusk 对象,而只会记录 fetch 找到的第一个 NSManagedObject 实例。

ByTheHusk[7880:207] (null)

Which means there are 207 entries. Is that right? and that ByTheHusk is getting passed correctly?

没有。在这种情况下,ByTheHusk 是应用程序/库的名称,数字是 IIRC 对象文件中的行和 block 偏移量。它们与被记录的对象无关。 (null) 仅表示语句 [[managedObject valueForKey:@"name"] description] 不返回任何类型的对象。

不确定您的问题是什么,但修复上面的第一段代码会有所帮助。

关于ios - 迭代传递给 View 的核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6679417/

相关文章:

ios - AFnetworking 不断返回响应代码=-1011“请求失败 : client error (422)

xcode - swift fatal error : Array index out of range

xcode - Xcode 6内存泄漏仪器不起作用

ios - 无法触摸 UIScrollview 后面的 UIButton

ios - 游戏在快速从后台恢复后退出暂停状态

ios - 如何限制 HH :MM:SS Formatter? 中的 textField 最大值

Xcode 4 final - 如何删除文档中的书签?

iphone - 表格没有倒数;这是一个高级设置(没有对象可以在特定关系的多个目的地)

objective-c - 核心数据是否在后台进行自己的类型转换?

swift - Xcode 7 "Extra argument ' 调用中的错误“核心数据错误”