NSArrayController初始化 [英] NSArrayController initialization
问题描述
我无法让支持核心数据的NSArrayController在我的代码中正常工作.下面是我的代码:
I am having trouble getting an core-data backed NSArrayController to work properly in my code. Below is my code:
pageArrayController = [[NSArrayController alloc] initWithContent:nil];
[pageArrayController setManagedObjectContext:[self managedObjectContext]];
[pageArrayController setEntityName:@"Page"];
[pageArrayController setAvoidsEmptySelection:YES];
[pageArrayController setPreservesSelection:YES];
[pageArrayController setSelectsInsertedObjects:YES];
[pageArrayController setClearsFilterPredicateOnInsertion:YES];
[pageArrayController setEditable:YES];
[pageArrayController setAutomaticallyPreparesContent:YES];
[pageArrayController setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES]]];
BOOL result = [pageArrayController setSelectionIndex:0];
当我尝试调用setSelectionIndex:时,它返回YES,表明选择已成功更改.但是,随后对pageArrayController对象的所有getSelectionIndex调用都返回NSNotFound.
When I attempt to call setSelectionIndex:, it returns YES, indicating that the selection has been successfully changed. However, any subsequent getSelectionIndex calls to the pageArrayController object returns NSNotFound.
我不明白的是,如果我将NSArrayController放入NIB中,并允许NIB文件执行初始化(在Interface Builder中具有所有相同的属性),则NSArrayController可以正常工作.
What I don't understand is that if I put the NSArrayController into a NIB, and allow the NIB file to perform the initialization (with all of the same attributes in Interface Builder), the NSArrayController works correctly.
为什么行为有所不同? NIB文件是否以特殊方式初始化这些类型的对象?我对NSArrayController的初始化不正确吗?
Why is there a difference in behavior? Does the NIB file initialize these types of objects in a special way? Is my initialization of the NSArrayController incorrect?
感谢您的帮助.谢谢.
推荐答案
是的,笔尖确实以一种特殊的方式初始化对象,有时很难弄清楚如何复制它们.我也为此付出了很多努力,终于在Apple的《核心数据编程指南》 >>核心数据和Cooca绑定>>中找到了答案.可可粉列表).答案是,如果使用零内容初始化一个arraycontroller,则还需要执行提取.
Yes, nibs do initialize objects in a special way and sometimes it can be hard to figure out how to replicate that. I struggled with this too and finally found the answer in Apple's Core Data Programming Guide >> Core Data and Cooca Bindings >> Automatically Prepares Content Flag (thanks to Dave Fernandes on the Cocoa Dev list). The answer is that if you initialize an arraycontroller with nil content, you need to perform a fetch as well.
BOOL result;
NSArrayController *pageArrayController = [[NSArrayController alloc] initWithContent:nil];
[pageArrayController setManagedObjectContext:[self managedObjectContext]];
[pageArrayController setEntityName:@"Page"];
NSError *error;
if ([pageArrayController fetchWithRequest:nil merge:YES error:&error] == NO)
result = NO;
else
{
//do all that other pageArrayController configuration stuff
result = [pageArrayController setSelectionIndex:0];
}
顺便说一句,[NSSortDescriptor sortDescriptorWithKey:@"index"升序:是]]发出警告.
BTW, [NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES]] raises a warning.
这篇关于NSArrayController初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!