[英]NSArray with Custom Objects in NSTableDataSource
I am trying to make NSTableDataSource compatible object and give this object to NSTableView as DataSource, however when table tries to display data, it crashes. 我正在尝试使NSTableDataSource兼容对象并将此对象作为数据源提供给NSTableView,但是当表尝试显示数据时,它会崩溃。
@interface NSArrayDataSource : NSObject{
NSArray* internalArray;
}
-(id) initWithArray: (NSArray*) objects;
-(int)numberOfRowsInTableView:(NSTableView *)aTableView;
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
-(void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
@end
@implementation NSArrayDataSource
-(id) initWithArray: (NSArray*) objects{
if(self = [super init])
{
internalArray = [[NSArray alloc] initWithArray:objects];
}
return self;
}
-(int)numberOfRowsInTableView:(NSTableView *)aTableView{
return [internalArray count];
}
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex{
id obj = [internalArray objectAtIndex:rowIndex];
// when I debug, I get same pointers with invalid data
// each object has "name" message
// this following line gives invalid pointer and
// it crashes
return [obj name];
}
-(void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex{
}
@end
Can anyone tell me what could be the problem? 谁能告诉我可能是什么问题?
Right now I have removed every release and dealloc messages from my class, so no object is removed, I have done retain message call everywhere when I transfer data. 现在,我已经从类中删除了每个版本和dealloc消息,因此没有对象被删除,在传输数据时,我在所有位置都完成了保留消息调用。
NSArrayDataSource* nsds = [[NSArrayDataSource alloc] initWithArray: myArray];
[tableView setDataSource:nsds];
I have one doubt, does NSArray does call [retain] for every objects while creating new object or it is just storing pointer values? 我有一个疑问,NSArray在创建新对象时是否会为每个对象调用[retain]还是只是存储指针值?
Suppose my object's name is "SLProject" then when I debug, in init method I see values under debug window correctly but in tableView delegation method the internalArray's debugger displays object of type "NSKVONotifying_SLProject" and they point to invalid data, however the addresses are correct. 假设我的对象的名称是“ SLProject”,那么当我调试时,在init方法中我可以正确看到调试窗口下的值,但是在tableView委托方法中,internalArray的调试器显示的对象类型为“ NSKVONotifying_SLProject”,并且它们指向无效的数据,但是地址正确。
In answer to your question about NSArray
object creation, yes, each object receives a retain
message when it is added to the array, and a release
message when it is removed. 是的,在回答有关NSArray
对象创建的问题时,是的,每个对象在添加到阵列时都会收到retain
消息,而在删除时会收到release
消息。 From the Apple docs: 从Apple文档中:
In general, objects that you add to an array aren't copied; 通常,不会复制添加到数组中的对象。 rather, each object receives a retain message before its id is added to the array. 相反,每个对象在将其ID添加到数组之前都会收到保留消息。 When an object is removed from an array, it's sent a release message. 从数组中删除对象时,会发送释放消息。
As for your object's name
function, how is it implemented? 至于对象的name
功能,如何实现? If you replace [obj name]
with [obj description]
does this still crash your application? 如果将[obj name]
替换为[obj description]
,这是否还会使您的应用程序崩溃?
My first suspicion would be that the -name NSString property of the SLProject class is not being retained. 我的第一个怀疑是SLProject类的-name NSString属性没有保留。 It would help to know the message in the debug log that describes the crash because without that we are just guessing. 了解调试日志中描述崩溃的消息会有所帮助,因为如果没有该消息,我们只是在猜测。
As for the "NSKVONotifying_" prefix on your class name, at runtime Cocoa will create a subclass of your object when you are using Key Value Observing. 至于类名上的“ NSKVONotifying_”前缀,在运行时,当您使用键值观察时,Cocoa将创建对象的子类。 This is normal and is unlikely to be the cause of your problem. 这是正常现象,不太可能是造成问题的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.