4

I am working on an ios app in which in the first viewcontroller the user is asked to provide its name gender age and native language. Then i will use those objects during the whole app. So what is the best way to store the objects so they are easilly accessible from all classes?

6 Answers6

3

Use NSUserDefaults. Really simple to use and can be accessed from anywhere in the application.

You should use it to store user preferences. It can store simple data types as well as complex objects and even array, dictionaries. To store any key would be as simple as,

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

And reading it back,

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

Note: User defaults are stored in a plist file and gets loaded each time application launches. So be careful to not write too much information into it. Otherwise the application loading time increases and if it goes beyond watchdog timer interval, the app will receive SIGKILL and crash.

But in your case, for storing user settings you can go ahead and use this.

Hope that helps!

Amar
  • 13,202
  • 7
  • 53
  • 71
  • 3
    I'd like to add that user defaults should be used for just that - user defaults! It should not be used as a storage container for usual program flow. – Eiko Aug 27 '13 at 07:34
3

Create singleton class say MySingletonClass and define your variable. you can access your variable from any class by singleton object.

// MySingleton.h
@interface MySingletonClass : NSObject

{
    //your global variable here
}

// set property for your variable

+ (MySingletonClass*)sharedInstance; // method for getting object of this singleton class

// In MySingleton.m
//synthesize property for your global variable


+ (MySingletonClass*)sharedInstance
{ 

  if ( !sharedInstance)
  {
      sharedInstance = [[UIController alloc] init];

   }
 return sharedInstance;
}

Now in other class you can access this variable. follow this code to access variable define in MySingletonClass

MySingletonClass* sharedInstance = [MySingletonClass sharedInstance]; // get object of MySingletonClass
sharedInstance.yourVariable; // now you can access variable defined in MySingletonClass by this object.

You can also definde global variable in AppDelegate (not recommended ).

Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
2

You can use a Singletonfor this. Such a class can only have one object and you can access it from anywhere within your app. Have a look at this question: Singleton

You just have to change the isMultiplayer property to match your needs.

Community
  • 1
  • 1
chris13
  • 640
  • 5
  • 14
1

create a class which represents:

…name gender age and native language

When you get the information from the user, create an instance of the class (e.g. immutable instance), and pass that from the view controller to the the other implementations which depend on it. In some cases, you will need to create ivars or properties to hold on to this instance.

No global is required.

justin
  • 104,054
  • 14
  • 179
  • 226
0

You can make extern any variable in appDelegate class use it anywhere in your project.

extern NSString *passString;

0

Yah I hope using NSUserDefaults is the best and apt way for the app mentioned above.its is very simple and easy to access the data from any view controller in the app without any higher difficulty.