【发布时间】:2009-06-21 07:31:18
【问题描述】:
我有一些代码需要访问 NSArray 才能工作。我有一个与 Core Data 一起使用的 NSArray,其中会有数据,但我不确定如何让我的 NSArrayController 访问 NSArray。
我不能像这样简单地在头文件中声明它:NSArray *objectArray;,因为它不知道如何访问或访问哪个NSArray。我将如何访问与 Core Data 一起使用的 NSArray?
我的头文件:
#import <Cocoa/Cocoa.h>
@interface MyOutlineView : NSOutlineView {
NSArrayController* objectArray;
}
@end
我的实施文件:
#import "MyOutlineView.h"
@implementation MyOutlineView
- (void) outlineView: (NSOutlineView *) aView
willDisplayCell: (id) aCell
forTableColumn: (NSTableColumn *)aColumn
item: (id) anItem
{
id rootObj = anItem;
unsigned row = [aView rowForItem:anItem];
[aCell setDrawsBackground: YES];
while ([aView levelForRow:row] != 0) {
row --;
rootObj = [aView itemAtRow:row];
}
// The colours here are foul and ugly. Use something else, for
// God's sake!
if( [objectArray indexOfObject:rootObj] % 2 )
[aCell setBackgroundColor: [NSColor yellowColor]];
else
[aCell setBackgroundColor: [NSColor blueColor]];
}
@end
【问题讨论】:
-
您以何种方式将 NSArray 与 Core Data 结合使用?是否要将 Core Data 对象提取到 NSArray 对象中?
-
我正在使用 NSArray 来存储大纲视图中的文本。我有一些代码可以使大纲视图中的根对象具有不同的背景颜色。但它需要访问 NSArray 以确定哪个是根对象。
-
现在什么对象持有 NSArray?你的 Nib 中有一个 NSArrayController 来控制 NSArray 吗?这可能是有道理的,这样其他对象就会绑定到 NSArrayController。
-
一个 NSArray... 来存储文本?另外,您知道实体不能具有有序集合属性,对吗?您必须创建并执行提取请求,如果您希望对象以非随机顺序排列,则需要为提取请求提供排序描述符。
-
嗯,这不仅仅是文字,但这不相关。我的 Nib 文件中确实有一个 NSArrayController 来控制 Array。
标签: objective-c cocoa nsarray