objective c - EXC_BAD_ACCESS from outlineView:child:ofItem -


please explain why having problems line below have commented out.

it causing exc_bad_access in outlineview:objectvaluefortablecolumn:byitem:.

a gist full class at: https://gist.github.com/onato/9d12bbbf5c4135673f24

- (id)outlineview:(nsoutlineview *)outlineview child:(nsinteger)index ofitem:(id)item {     if (!item) {         item = self.data;     }     id returnvalue = @"";     if ([item iskindofclass:[nsarray class]]) {         returnvalue = @"value";//[item objectatindex:index];     }  //    return @{@"index":@(index), @"value":returnvalue}; // produces exc_bad_access in outlineview:objectvaluefortablecolumn:byitem:     return returnvalue; } 

i have tried creating basic project datasource , nothing else , still see problem.

you can't make items on fly in outlineview:child:ofitem:. of items must exist, or @ least continue exist afterward until deleted (i.e., removed on user's behalf both view and model) or until whatever outline view showing dismissed (e.g., document closed).

dictionary literals (@{ … }) represent creation of dictionary @ point. whenever program gets line, it create new dictionary, every time, same child of same item. (this true when dictionary includes isn't constant, such values of both index , returnvalue.)

even if keep dictionaries around, though, using plain old dictionaries and/or arrays model makes hairy code quickly.

the solution

make simple subclass of nsobject 2 properties:

  • value (or more specific), whatever kind of value you're showing in outline view
  • children (or more specific), array of whatever descendant items each item may contain

then keep array of objects. when asked child of nil, return 1 of objects in array. when asked child of item, item 1 of these objects, return 1 of children.

when asked object value (for assume 1 , column) of item, return item's value. if have multiple columns, have property each column.

most importantly, create of these objects before outline view visible , keep them around until done them (whether deletion of item or dismissal of view). don't create items needed , expect outline view hang on them you—that's not job; that's your job controller.

the items don't have same class; if makes sense app, can have foos owning bars , bars owning bazzes. if needs simple, may make more sense foo. whichever makes sense. either way, custom objects add clarity code.

more point, it's more obvious when you're dropping custom object on floor versus dropping dictionary created literal. it's easier see return [[foo alloc] init…] , reminded “oh, right, need hang on that”.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -