简体   繁体   中英

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.

@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.

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?

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.

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. From the Apple docs:

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. 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? If you replace [obj name] with [obj description] does this still crash your application?

My first suspicion would be that the -name NSString property of the SLProject class is not being retained. 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. This is normal and is unlikely to be the cause of your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM