In iOS apps, there are often values and variable sharing in different view and applications, and there are several ways to do this:
1.extern mode
2. Single-case mode
3.delegate mode
The singleton pattern, as the name implies, is only one instance, which ensures that a class has only one instance, and instantiates it itself and provides it to the system as a whole. It is often used to do application-level shared resource control. This mode is used very frequently, and through a singleton class, you can implement parameter passing between different view
#import <Foundation/Foundation.h> @interface session:nsobject@property (strong,nonatomic) NSString * singlevalue;//implementation of a single case method + (Session *) getinstance; @end
#import "Session.h" @implementation session//single-instance static Session *instance;//Singleton + (Session *) getinstance { @ Synchronized (self) { if (instance = = Nil) { instance = [[Self alloc] init]; } } return instance;} -(ID) init{ if (self = [super init]) { self.singlevalue = [[NSString alloc] init]; } return self;} @end
Then, in the case where you need to use a singleton class import, this singleton class
Session *session = [session getinstance];
Session.singlevalue = @ "Good magic!" ";
NSString *value = Session.singlevalue;
Single-pass value of iOS value