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

iOS

How to have a central “data” class

Is there a way to make a class that the entire program can access and modify? Meaning lets say i have 3 views. First view gets your first name, second view gets your last name and the 3rd view displays them both. Is there a way that i can make a class where i can do something like NameClass.firstName = @“FirstName” and in the second view NameClass.lastName = @LastName” then in the third view be able to access the data from that class and output it by like accessing the NameClass and those variables?

3 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Alex,

You can do this a few ways. The first would be to have a class with class methods exclusively, thus creating a "static" class. You can't have properties as you would with an instance but you can create methods like getFirstName or setFirstName, for example. Class methods are methods that being with a + sign.

+ (void) classMethodName
{
   // Implementation
}

These class methods deal directly with static fields of the class. You can read more about this here.

Another way would be using what's called the singleton pattern. You can read all about it here. Basically, this will allow you to have a single, stageful class that can be accessible anywhere. An example of this can be found here.

From what I've read and seen the singleton pattern is the more common technique of the two due to potential memory leaks using the "static" class approach.

I hope I understood your question correctly. If not please let me know. I hope this helps.

another way would be to use the app delegate and add some properties to that class. The app delegate is accessible anywhere using

[[UIApplication sharedApplication] delegate]

so you could add some properties in appDelegate.h alloc/init them in didFinishLaunchingWithOptions then set/get them with

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.firstName = @"blah";

it might already be a singleton class that justin mentioned. Im not really sure. and your app delegate is probably named something like AAAppDelegate.h/m or something similar, so you would have to account for that

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Another option I would like to point out is NSUserDefaults. If you want to save user or application data for your app you don't have to create a class to hold this data yourself. iOS has a defaults system built-in.

Saving data using NSUserDefaults

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:firstName forKey:@"firstName"];

And loading data using NSUserDefaults

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *firstName = [defaults objectForKey:@"firstName"];

You can see a sample of NSUserDefaults here and read the official documentation here.

I hope this helps.