iphone - How to share an array between two classes? -


i want array gets data 1 class avaliable me in class same data.
if declare array on applicationdelegate class.
declare object of applicationdelegate in both classes.
, assign array appdelegate.array 1 class, able array across classes?

i'm mike. leave app delegate out of it.

you're in control of when , how objects instantiated. if it's important more 1 class have access same data, hand off data, or means of getting @ data, create instances of dependent class.

an example, given classes wvparent , wvchild.

wvparent has property, somearray, array other objects need:

@interface wvparent : nsobject {   nsarray *somearray  } @property (nonatomic, retain) nsarray *somearray;  @end 

then have wvchild, has property called parentobject:

@class wvparent;  @interface wvchild : nsobject {   wvparent *parentobject;  } @property (nonatomic, assign) wvparent *parentobject;  @end 

assuming parent creating child instance, you'd allocate , assign parent:

wvchild *child = [[wvchild alloc] init]; child.parentobject = self; 

then child instance can access somearray by:

self.parentobject.somearray 

this 1 option. pass array child, assuming data static , unlikely change on course of application session.

this way, instead of having source of data living somewhere in app delegate, lives within class more appropriately responsible creation, maintenance , vending. if you've got class pops existence , can reach rest of app getting app delegate, might want put little more thought architecture.

more on dependency injection here:

http://en.wikipedia.org/wiki/dependency_injection


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -