Review-singleton mode, review-Mode
1. Singleton)
* Ensure that only one object is created for a class.
2. Role
* Saves memory overhead.
* If some data is used in the entire program, you only need to use the same resource (ensure that the data accessed by everyone is the same)
* Generally, the tool class is designed as a singleton mode.
3. Implementation
* MRC
* ARC
SoundTool. h
1 # import <Foundation/Foundation. h> 2 3 @ interface SoundTool: NSObject <NSCopying> 4 5 + (instancetype) Explain SoundTool; 6 7 @ endView Code
SoundTool. m
# Import "SoundTool. h "@ implementation SoundToolstatic id _ instance = nil; + (instancetype) allocWithZone :( struct _ NSZone *) zone {if (_ instance = nil) {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {_ instance = [super allocWithZone: zone] ;});} return _ instance ;}+ (instancetype) export soundtool {return [[self alloc] init];}-(instancetype) init {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {_ instance = [super init];}); return _ instance;} + (instancetype) copyWithZone :( struct _ NSZone *) zone {return _ instance;} + (instancetype) mutableCopyWithZone :( struct _ NSZone *) zone {return _ instance;} // The following three are not used by ARC-(oneway void) release {}-(instancetype) retain {return _ instance;}-(NSUInteger) retainCount {return 1 ;}View Code
4. It is recommended to package it into a macro.