Tiko  Harutyunyan
Tiko Harutyunyan

Reputation: 117

Storing global objects in IOS apps?

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?

Upvotes: 4

Views: 1305

Answers (6)

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.

Upvotes: 0

justin
justin

Reputation: 104698

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.

Upvotes: 1

Amar
Amar

Reputation: 13222

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!

Upvotes: 3

Mayank Jain
Mayank Jain

Reputation: 5754

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 ).

Upvotes: 3

user2383700
user2383700

Reputation: 11

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

extern NSString *passString;

Upvotes: 0

chris13
chris13

Reputation: 640

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.

Upvotes: 2

Related Questions