What's the best way to have functions share an array in Objective-C?
I understand that in Objective-C you declare an array in the header file and interact with it in a class. So far I'm adding things and fetching them fine within a single function. I'm new to the language however and can't figure out how 开发者_运维技巧to share that array across other functions.
I'd like to initialize array data in my viewDidLoad and access it from various functions later on. Is this possible and if so what's the best way to do it?
Like you said, declare the array in the view controller's header file and make it a @property
. Use alloc-init in the implementation's -viewDidLoad
method to set it up. Deallocate it in the dealloc
method. Use its property setter (self.array
) to retain or assign another array, depending on the @property
attribute. Access it directly (array
) throughout your methods in your class implementation, and via its property getter (obj.array
) from other classes.
精彩评论