#import <Foundation/Foundation.h> @interface fucardsettings:nsobject+ (fucardsettings *) sharedinstance; @end
#import "FuCardSettings.h" @implementation FuCardSettings//================= single Case =========== === startstatic fucardsettings *sharedsettings = nil;+ (FuCardSettings *) Sharedinstance { if (sharedsettings == nil) { sharedsettings = [[[self alloc] init]autorelease]; } return sharedsettings;} + (ID) Allocwithzone: (nszone *) zone { @synchronized (self) { if (Sharedsettings == nil) { sharedsettings = [super allocwithzone: Zone]; } }&nBsp; return sharedsettings;} + (ID) Copywithzone: (nszone *) zone { return self;} - (ID) retain { return self;} - (Nsuinteger) Retaincount { return nsuintegermax;} - (oneway void) release {}- (ID) autorelease { return self;} ================= Single Case ============== end@end
The above is a traditional single case:
There is also a single example that is implemented with GCD:
#import <foundation/foundation.h>@ Interface tool1 : nsobject@property (nonatomic,copy) nsstring *test;+ (instancetype) instance, @end @implementation tool1+ (instancetype) instance{ static tool1 * tool; static dispatch_once_t oncetoken; Dispatch_once (&oncetoken, ^{ tool = [[tool1 alloc] init]; }); return tool;} - (instancetype) init{ self = [super init]; if (self) { _test = @ "test"; } return self;} @end
The notation has the following characteristics:
1. Thread safety.
2. Meet the requirements of the static analyzer.
3. Compatible with ARC
iOS single case