我正在使用此方法将数据传递到 View :
nextViewController = [[AfricanSwallowViewController alloc] initWithNibName:@"AfricanSwallowView" bundle:nil];
((InstructionsViewController *)nextViewController).byTheHusk = byTheHusk;
我正在尝试迭代 byTheHusk 传递到的 View 中的值。
我已经尝试了多个版本的 NSLog(@"%@", byTheHusk.name);
并使用了 objectAtIndexPath
等,但没有成功
有没有办法在 while
或 for
循环样式结构中访问对象条目属性?
我能得到的最接近的是:
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/