Storing global objects in iOS apps?

I am working on an ios application in which the first view of the control prompts the user to indicate his gender age and native language. Then I will use these objects throughout the application. So what is the best way to store objects so that they are easily accessible from all classes?

+4
source share
6 answers

Use NSUserDefaults . Actually easy to use and accessed anywhere in the application.

You must use it to store user settings. It can store simple data types, as well as complex objects and even arrays, dictionaries. To store any key will be easier than

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

And after reading it,

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

Note Custom defaults are stored in the plist file and loaded each time the application starts. Therefore, be careful not to write too much information into it. Otherwise, the application load time increases, and if it goes beyond the watchdog timer, the application will receive SIGKILL and crash.

But in your case, you can use this to store user settings.

Hope this helps!

+3
source

Create a singleton class, specify MySingletonClass and define your variable. you can access your variable from any class using a 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 another class you can access this variable. follow this code to access the define variable 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 define a global variable in AppDelegate (not recommended).

+3
source

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

You just need to change the isMultiplayer property to suit your needs.

+2
source

create a class that represents:

... name age of gender and mother tongue

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

Global is not required.

+1
source

You can force extern to any variable in the appDelegate class to use it anywhere in your project.

extern NSString * passString;

0
source

I hope that using NSUserDefaults is the best and most convenient way for the application mentioned above. This is a very simple and easy access to data from any view controller in the application without any difficulties.

0
source

Source: https://habr.com/ru/post/1499081/


All Articles