我对核心数据没有太多经验,但我已经广泛使用了 Cocoa 的绑定。以我的经验,这样的东西最适合“手动”调整的中间NSArray
对象。
作为一个例子,我想你会有一个自定义NSArray
的用户生成的播放列表,你也会有一个单独NSArray
的包含你的标准库项目。通过此设置,我将在您的一个名为 的控制器中创建一个属性combinedArray
,该属性将绑定到您的NSOutlineView
. 然后,我会将您的用户生成的播放列表数组绑定到控制器,并让控制器中的一些代码在播放列表数组被修改时自动更新组合数组。
这是一个快速的模型:
控制器.h
@interface TheController : NSObject
{
NSArray * combinedArray;
NSArray * userPlaylists;
}
@property (retain) NSArray * combinedArray;
@property (copy) NSArray * userPlaylists;
@end
控制器.m
@implementation TheController
@synthesize combinedArray;
@synthesize userPlaylists;
- (void)setUserPlaylists:(NSArray *)newLists
{
// standard property setting code:
if (newLists != userPlaylists)
{
[userPlaylists release];
userPlaylists = [newLists copy];
}
// modify the combined array:
NSMutableArray * allItems = [NSMutableArray arrayWithCapacity:0];
[allItems addObjectsFromArray:standardLibrary];
[allItems addObjectsFromArray:userPlaylists];
[self setCombinedArray:allItems];
}
@end