ios - Accessing obj as property vs method param (style preferences) -
when comes accessing objects different methods in same class, understand, these 2 ways it. given do want hold property pointer object, better way go this? i've been thinking while, , wondered if there preference consensus.
#1:
nsarray *array = ... // array somewhere self.myarray = array; [self dosomethingtomyarray];
this method takes no parameter , accesses array via own property via self
- (void)dosomethingtomyarray { // stuff with/to array via self.myarray [self.myarray ...]; }
vs #2:
nsarray *array = ... // array somewhere self.myarray = array; [self dosomething:array];
this method takes array , accesses array via own method parameter
- (void)dosomething:(nsarray *)array { // stuff with/to array via method parameter "array" [array ...]; }
i think it's going depend on dosomethingtomyarray
, calls it.
fairly obvious comments:
- if want more 1 array, need take argument;
- if you're doing logically more array class (e.g. you've implemented randomisation of order of array) it'd better category on
nsarray
rather being anywhere in class @ all; - if there's possibility of subclasses wanting redefine manner in array obtained you'll want invoke getter somewhere;
- similar concerns apply if subclass or external actor should able intercede anywhere else in process.
beyond concerns there bunch of prefer stuff functional anyway — noting you're doing to array, not with array, in case you'd tend more towards self.myarray = [self processedformof:array]
(or self.myarray = [array arraybydoingsomething];
if category tip makes sense).
so, ummm, don't think there's clear-cut answer.
Comments
Post a Comment