如何以编程方式使用CoreData行填充NSArrayController? [英] How do you populate a NSArrayController with CoreData rows programmatically?

查看:212
本文介绍了如何以编程方式使用CoreData行填充NSArrayController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在几个小时/天的搜索和潜水到示例项目后,我得出结论,我需要问。如果我将assetsView( IKImageBrowserView )直接绑定到 NSArrayController 的I​​B实例,一切都很好。

After several hours/days of searching and diving into example projects I've concluded that I need to just ask. If I bind the assetsView (IKImageBrowserView) directly to an IB instance of NSArrayController everything works just fine.

- (void) awakeFromNib
{
    library = [[NSArrayController alloc] init];
    [library setManagedObjectContext:[[NSApp delegate] managedObjectContext]];
    [library setEntityName:@"Asset"];       

    NSLog(@"%@", [library arrangedObjects]);
    NSLog(@"%@", [library content]);

    [assetsView setDataSource:library];
    [assetsView reloadData];
}

NSLogs 空。我知道我错过了什么...我只是不知道什么。目标是最终允许此视图的库的多个实例使用谓词以编程方式过滤。现在我只是试图让它显示资产实体的所有行。

Both NSLogs are empty. I know I'm missing something... I just don't know what. The goal is to eventually allow multiple instances of this view's "library" filtered programmatically with a predicate. For now I'm just trying to have it display all of the rows for the "Asset" entity.

添加:如果我创建 NSArrayController 在IB中,然后尝试日志 [library arrangedObjects] 或手动设置assetsView的数据源我获得相同的空结果。就像我之前说的,如果我绑定 library.arrangedObjects assetsView.content IKImageBrowserView )在IB中 - 具有与IB相同的受管对象上下文和相同的实体名称 - 一切都按预期工作。

Addition: If I create the NSArrayController in IB and then try to log [library arrangedObjects] or manually set the data source for assetsView I get the same empty results. Like I said earlier, if I bind library.arrangedObjects to assetsView.content (IKImageBrowserView) in IB - with same managed object context and same entity name set by IB - everything works as expected.

- (void) awakeFromNib
{
//  library = [[NSArrayController alloc] init];
//  [library setManagedObjectContext:[[NSApp delegate] managedObjectContext]];
//  [library setEntityName:@"Asset"];       

    NSLog(@"%@", [library arrangedObjects]);
    NSLog(@"%@", [library content]);

    [assetsView setDataSource:library];
    [assetsView reloadData];
}


推荐答案

你没有实际告诉NSArrayController来获取任何东西。 NSArrayController是空的,直到你通过绑定或手动添加对象。

It looks like the problem is that you have not actually told the NSArrayController to fetch anything. NSArrayControllers are empty until you add objects either through bindings or manually.

设置库后尝试调用其fetch方法:

After setting up library try to call its fetch method:

[library fetch:self];

此外,你可能已经知道了,但是可以使用下面的方法在代码中设置绑定:

Also, you probably know this already but it is possible to set bindings in code with the following method:

- (void)bind:(NSString *)binding toObject:(id)observableController withKeyPath:(NSString *)keyPath options:(NSDictionary *)options

这篇关于如何以编程方式使用CoreData行填充NSArrayController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆