Best way of singleton in Objective-C
It’s very useful during iOS development
static GeneralData __strong *generalDataObject = nil; |
Usage
Add Data
firstViewController.m #import "GeneralData.h";
- (IBAction) passValueButton:(id)sender {
{
User *user = [User new];
user.name = "Mary Jane";
user.age = "21";
[GeneralData sharedInstance].currentUser = user;
}Read Data
secondViewController.m #import "GeneralData.h";
- (void)viewDidLoad
{
[super viewDidLoad];
User *loggedInUser = [GeneralData sharedInstance].currentUser;
NSLog(@"Name : %@", loggedInUser.name);
NSLog(@"Age : %@", loggedInUser.age);
}
Reference : StackOverflow