Simple instance of iOS singleton mode

The single instance mode mainly realizes the unique instance, which survives in the whole program range. Generally, the single instance is often used to store user information, such as user password. The password is used once in the login interface, once in the password modification interface, and the single instance can ensure the unique instance of password. If the single instance mode is not used, the stack addresses of the two instances of init are different, so the location of the stored data is also different. When one of the data changes, the other data remains unchanged. The code of singleton mode is as follows

.h file

#ifndef Singleton_h
#define Singleton_h

@interface Singleton : NSObject
@property (nonatomic, copy) NSString *pass;
+ (Singleton *) sharedInstance;

@end

.m file

#import <Foundation/Foundation.h>
#import "Singleton.h"

@implementation Singleton
static id sharedSingleton = nil;
+ (id)allocWithZone:(struct _NSZone *)zone
{
if (sharedSingleton == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSingleton = [super allocWithZone:zone];    
});
}
return sharedSingleton;
}

- (id)init
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedSingleton = [super init];
    });
    return sharedSingleton;
}

+ (instancetype)sharedInstance
{
return [[self alloc] init];
}
+ (id)copyWithZone:(struct _NSZone *)zone
{
return sharedSingleton;
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone
{
return sharedSingleton;
}


@end

Macro implementation example

#ifndef Singleton_m_h
#define Singleton_m_h

// Help to realize single design pattern

// .h File implementation
#define SingletonH(methodName) + (instancetype)shared##methodName;

// .m File implementation
#if __has_feature(objc_arc) // yes ARC
#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}

#else // No ARC

#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
\
- (oneway void)release \
{ \
\
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return 1; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}
#endif /* Singleton_m_h */

Tags: iOS

Posted on Tue, 03 Dec 2019 09:02:09 -0800 by traxy