Reputation: 287
I have a bewildering problem, hoping someone can assist:
I have a model object, called Road. Here's the interface.
@@@
@interface RoadModel : NSObject {
NSString *_id;
NSString *roadmapID;
NSString *routeID;
NSString *title;
NSString *description;
NSNumber *collapsed;
NSNumber *isRoute;
NSString *staff;
NSNumber *start;
NSArray *staffList;
NSMutableArray *updates;
NSMutableArray *uploads;
NSMutableArray *subRoads;
}
@property (nonatomic, copy) NSString *_id;
@property (nonatomic, copy) NSString *roadmapID;
@property (nonatomic, copy) NSString *routeID;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *description;
@property (nonatomic, copy) NSNumber *collapsed;
@property (nonatomic, copy) NSNumber *isRoute;
@property (nonatomic, copy) NSString *staff;
@property (nonatomic, copy) NSNumber *start;
@property (nonatomic, copy) NSArray *staffList;
@property (nonatomic, copy) NSMutableArray *updates;
@property (nonatomic, copy) NSMutableArray *uploads;
@property (nonatomic, copy) NSMutableArray *subRoads;
- (id)initWithJSONObject:(NSDictionary *)JSONObject;
@end
This part is fine.
To give you some background, I'm translating a bunch of JSON into a proper model object so it's easier to work with.
Now, I'm trying to display this in an NSOutlineView. This is where the problem is. In particular, I have created the table and a datasource.
- (id)initWithRoads:(NSArray *)roads {
if (self = [super init])
root = [[NSMutableArray alloc] initWithArray:roads];
return self;
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
if (item == nil)
return root.count;
return 0;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return NO;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
if (item == nil)
item = root;
if (item == root)
return [root objectAtIndex:index];
return nil;
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
return [item title];
}
In the final datasource method, it attempts to return the "title" string property of the model object, but for some reason crashes each time. I have checked that the method is taking in the correct object (I checked [item class] description], and it is the right object), but for some reason if I call any of the objects accessors the app immediately crashes.
This is totally puzzling because in the init method, I can iterate through root (an array of RoadModel objects), and print any of its properties without issue. It is only when I'm trying to access the properties in any of the datasource methods that this occurs. I wonder if there is something memory-wise that is going on behind the scenes and I am not providing for it.
If you can shed some light on to this situation, it would be greatly appreciated!
Thanks in advance!
Upvotes: 1
Views: 719
Reputation: 86651
Usually, this kind of thing is caused by over-releasing of objects. By the time you get to the method that crashes, either your data source or your root array has been deallocated. Don't forget that NSOutlineView
maintains a weak reference to its data source. This means that in reference counted world it does not retain the data source and in GC world, the reference is not enough to stop the data source from being collected.
You need to maintain a retained/strong reference elsewhere.
Upvotes: 1