ios - NSMutableArray not being modified properly when manipulated in other class -
i have nsmutablearray property in view controller class, , line being used in 2 other classes. 1 of classes manipulates array (adds more objects it) , reads it. when 1 of classes manipulates array, not being modified in view controller class, instantiated in. therefore, third class not getting proper date needs.
in view controller class:
@property (nonatomic, strong) nsmutablearray *entitylines;
in other 2 classes:
@property (nonatomic, weak) nsmutablearray *linestodraw; @property (nonatomic, weak) nsmutablearray *linesforkey;
array initialization:
- (id)init { self = [super init]; if (self) { self.title = @"line graph"; linequery = [[linegraphquery alloc] init]; entitylines = [nsmutablearray array]; } return self; }
modifying array:
- (void)drawrect:(cgrect)rect { if(data.namedentitydata.count > 0) { cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsetlinewidth(context, line_width); cgcontextsetlinecap(context, kcglinecapround); cgcontextsetfillcolorwithcolor(context, [[uicolor blackcolor] cgcolor]); cgcontextfillrect(context, rect); [self clearalllines]; for(nsstring *key in [data.namedentitydata allkeys]) { entityline *entityline = [self getnamedentitylineforname:key]; if(!entityline) { entityline = [[entityline alloc] init]; entityline.name = key; entityline.color = [self getrandomcolor]; } float intervalx = starting_interval_x; float lastrangey = min_event_count_y; cgcontextsetstrokecolorwithcolor(context, [entityline.color cgcolor]); nsarray *events = [data.namedentitydata objectforkey:key]; nsinteger rangedifference = data.endyear - data.beginyear; for(int = 0; < numberofdaterangeintervals; i++) { int startyearrange = data.beginyear + (i * (rangedifference / numberofdaterangeintervals)); int endyearrange = (i == numberofdaterangeintervals - 1) ? data.endyear : data.beginyear + ((i + 1) * (rangedifference / numberofdaterangeintervals) - 1); int eventcount = [self getcountforevents:events withbeginyear:startyearrange andendyear:endyearrange]; line *line = [[line alloc] init]; line.begin = cgpointmake(intervalx, lastrangey); cgcontextmovetopoint(context, line.begin.x, line.begin.y); intervalx += intervalxincrement; lastrangey = [self getycoordinateforeventcount:eventcount]; line.end = cgpointmake(intervalx, lastrangey); [entityline addline:line]; cgcontextaddlinetopoint(context, line.end.x, line.end.y); cgcontextstrokepath(context); } [linestodraw addobject:entityline]; } [self draweventcountlabelswithcontext:context]; [self drawdaterangelabelswithcontext:context]; } } - (void)clearalllines { for(entityline *line in linestodraw) [line clearlines]; }
other class setting reference nsmutablearray:
linegraph.linestodraw = self.entitylines; linekey.linesforkey = self.entitylines;
giving properties (or instance variables) same name in different classes not cause them point same objects. after creating array, need pass pointer array property in other class instances.
@interface abcfirstclass () @property (nonatomic, strong) nsmutablearray *lines; @property (nonatomic, strong) abcanotherclass *otherclass; // has property named "lines". @end @implementation abcfirstclass - (id)init { self = [super init]; if (self) { self.lines = [nsmutablearray arraywithobjects:@"1", @"2", @"3", nil]; self.otherclass = [[abcanotherclass alloc] init]; self.otherclass.lines = self.lines; // both classes have pointer same array object. } return self; }
this doesn't need happen in -init method. maybe totally different class gets pointer 1 , passes other.
note i'd work ivars directly in -init (_lines, _otherclass), wanted keep example simple.
Comments
Post a Comment