Global variable in iOS TabBar application

I am creating an ios application in xcode 4.2. I have an external database file. I do not want to load data in all kinds. How to create a global variable for a tab application? And when should I download this database before closing the application?

+4
source share
3 answers

I use singletones as follows: in the DataBase class with some data arrays, I implement the share method:

+(id)share { static id share = nil; if (share == nil) { share = [[self alloc] init]; } return share; } 

and then in some classes: self.dataBase = [DataBase share];

+2
source

In iOS applications, model data is often stored in a singleton mode, rather than in a global variable. Here's an article that briefly describes singletones in Objective-C.

You can load your data in a class method that initializes your common singleton. Uploading data back is a bit more complicated, because singleton himself does not know when to do it. Therefore, you must make the instance method -(void)uploadData in your singleton class and call this method when your application is about to close. applicationWillResignActive: Your application delegate method is a good place to start the download.

+5
source

You can create global variables by doing this.

 extern NSString *someString; @interface ...... @property (strong, nonatomic) NSString *someString; @end @implementation ...... @systhesize someString; NSString *someString; @end 
0
source

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


All Articles