📜 ⬆️ ⬇️

MSLibrary. JUST: declare global variables using the singleton class, for iOS and not only ...

The developers of the MSLibrary for iOS library continue a series of very compact articles on how to just implement a particular function. No theory, just practice ...

So, how to correctly declare global variables? By global, we mean variables that are declared externally (non-static) and have a global lifetime.

If we declare a variable in one of the header files, for example, in the file MyClass.h, in such a seemingly logical way:
')
NSString *myGlobalVariable; 

and then import the MyClass.h file with the help of the #import directive into several files, for example, MyClass1.h, MyClass2.h, MyClass3.h and MyClass4.h, then as a result we are likely to get something like this error message:

 duplicate symbol _myGlobalVariable in: /Users/L/Library/Developer/Xcode/DerivedData/…/MyClass.o /Users/L/Library/Developer/Xcode/DerivedData/…/MyClass1.o duplicate symbol _myGlobalVariable in: /Users/L/Library/Developer/Xcode/DerivedData/…/MyClass.o /Users/L/Library/Developer/Xcode/DerivedData/…/MyClass2.o duplicate symbol _myGlobalVariable in: /Users/L/Library/Developer/Xcode/DerivedData/…/MyClass.o /Users/L/Library/Developer/Xcode/DerivedData/…/MyClass3.o duplicate symbol _myGlobalVariable in: /Users/L/Library/Developer/Xcode/DerivedData/…/MyClass.o /Users/L/Library/Developer/Xcode/DerivedData/…/MyClass4.o ld: 4 duplicate symbols for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 

This will happen due to the fact that as a result of the import, the variable myGlobalVariable will be declared in each of the connected files.

You can declare a global variable using the “magic” word extern. In this case, we will most likely avoid error messages, but for variables with global lifetime and scope, the entire application space, there is a danger of trying to simultaneously change its value from different streams, from which the extern cannot provide guaranteed protection. Unfortunately, inexperienced programmers do not take into account this important fact. Extern can be safely used when declaring global constants, in this case there are no problems.

We need to declare a global variable so that it is not duplicated under any circumstances. To solve this simple task, it suffices to recall what can exist in the program only in a single copy? That's right, this is singleton. A global variable can be set using the singleton class (singleton), or rather, as its property. A lot of literature has been written about the merits of this approach, and all those who are interested can read about it, and also about when such merits are fully manifested. Our task is to show how this is done.

Pattern Singleton: Verifies that there is only one instance of the class and provides a single point of access to it. (GoF Design Patterns (Addison-Wesley, 1994)).

Below is an example of code - we create a class sington, for example, GlobalVariable and add the property myGlobalVariable to it, which will be our global variable:

Header file, GlobalVariable.h

 #import <Foundation/Foundation.h>; @interface GlobalVariable : NSObject @property(nonatomic, strong) NSString *myGlobalVariable; +(instancetype)sharedGlobalVariable; @end 

GlobalVariable.m file

 #import "GlobalVariable.h" @implementation GlobalVariable @synthesize myGlobalVariable; +(instancetype)sharedGlobalVariable { static id _singletonInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _singletonInstance = [[super allocWithZone:NULL] init]; }); return_singletonInstance; } +(id) allocWithZone:(NSZone*)zone { return [self sharedGlobalVariable]; } -(id) copyWithZone:(NSZone*)zone { return self; } 

The reference to the variable is as follows:

 [GlobalVariable sharedGlobalVariable].myGlobalVariable; 

For convenience, you can use the macro:

 #define MY_GLOBAL_VARIABLE [GlobalVariable sharedGlobalVariable].myGlobalVariable; 

The dispatch_once_t function is responsible for the thread safety of this code implementation.
As you can see, the issue is solved in just a few simple lines, and when using the MSLibrary for iOS library, the amount of code will decrease several times.



We hope that the material was useful to you, the MSLibrary for iOS team

Other articles:
Capture and verify phone numbers using regular expressions, for iOS and not only ... Part 1
Capture and verify phone numbers using regular expressions, for iOS and not only ... Part 2
Implementing multiple selection of conditions using bitmasks, for iOS and not only ...
SIMPLE: remove unnecessary characters from strings using regular expressions for iOS and not only ...
Creating and compiling cross-platform (universal) libraries in Xcode

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


All Articles