Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Stephen Hancocks
9,271 PointsAccessing an array property from another class.
How can set up an array property so that I can access it from anywhere in my application at any time?
Ok, I've taught myself objective-C by watching Treehouse videos and using stackoverflow etc.
I'm doing ok but I'm struggling with something that I think should be fundamental.
I want to store a number of arrays that can accessed across my application [say 10+ view controllers]. Should I use properties or global variables?
I can get the app to work with global variables, but when I comes to using properties I struggle.
I need to set up all the arrays at the time of the initial view. That all works fine and when I NSLog the contents it's there. When I go to a different view controller however I cannot access my model array.
I'm aware that I don't want to create a new instance of the model class as that's not where the original instance variables are.
If I should use global variables then I will. However I see a lot of the treehouse videos using @property and assumed [maybe wrongly] that that was the way to go.
Thanks Steve
3 Answers
Kevin Hamil
8,203 PointsI'm fairly new to iOS development, but I think this should be pretty straight forward as you said.
It makes sense to me to use @property. Anytime an implementation file needs to access a property, you have to import the header file that includes that property. So if all of your arrays are setup as various properties in one header file... import that header file into all the implementation files that need to access those arrays. Then you should just be able to access them with self.propertyName.
I believe that's right, but if I'm wrong, please someone correct me.
Stephen Hancocks
9,271 PointsHi Kevin, thanks for taking the time to respond.
One would hope it would be that simple.
self.propertyName only allows access to properties in the corresponding header file.
I download all my data [takes around 20 seconds] and set up my arrays in ClassAModelObject. The user then navigates through the app to choose which layout they want to view the arrays in [lets say ClassDViewcontroller]. Then, to access the data I am trying to us the methods Amit Bijlani uses in the Crystal Ball app.
I have ClassD set up as so
#import "ClassDViewcontroller.h"
@class ClassAModelObject;
@interface ClassDViewcontroller : UIViewController
@property (nonatomic) ClassAModelObject *modelObject;
@property (nonatomic) IBOutlet UILabel *numberLabel;
I have ClassD implementation set up as so
#import "ClassDViewcontroller.h"
#import "ClassAModelObject"
@interface ClassDViewcontroller ()
@end
@implementation ClassDViewcontroller
- (void)viewDidLoad {
[super viewDidLoad];
self.numberLabel.text = [[self.modelObject myArray] objectAtIndex:3];
}
That is how I wanted to load the information, however it does not call the method which returns the array.
When I add
self.modeObject = [[ClassAModelObject alloc] init];
it calls the method but on a new instance so the previous arrays I set up are lost.
As I said in my earlier post I think this should be fundamental and it's likely something simple that I've overlooked. If I can't get it to work I'll go back to using the global variables that I used successfully before. I just felt from the videos on here that properties should be used.
Thanks.
Stephen Hancocks
9,271 PointsOk. Just having my morning shower [I'm in the UK] and it clears the mind allowing me think better.
I think I've been going about it the wrong way. It's the data used to form the array, not necessarily the formed array itself that I want to keep around. In reality I can form the arrays when required in each view controller.
So - I'm either going to save the initial downloaded information [likely into NSUserDefaults as it's only 2 files and I'm not up to speed with core data] or I'll use the global variables I mentioned earlier and form the arrays just once.
Do Apple have any preference to how apps are built? I don't want to do it all for it then to be rejected.
Thanks