IOS-3-Objective-C language: Singleton Design Model
Abstract: The method of the parent class needs to be rewritten in the singleton design mode. Because Global instances cannot be released, the retain, release, and autorelease methods related to memory management need to be rewritten to avoid Memory operations on this instance, prevents the reference count from changing.
The specific instance code in singleton mode is as follows:
SingletonTeather. h file
# Import
@ Interface SingletonTeather: NSObject
@ Property (nonatomic, strong) NSString * name;
// @ Property (nonatomic, assign) NSInteger age;
@ Property (nonatomic, strong) NSMutableDictionary * info;
+ (SingletonTeather *) getSingletonTeather;
@ End
SingletonTeather. c file
# Import SingletonTeather. h
StaticSingletonTeather * shareSingletonTeather = nil;
@ Implementation SingletonTeather
@ Synthesize name, info;
+ (SingletonTeather *) getSingletonTeather
{
If (shareSingletonTeather = nil ){
ShareSingletonTeather = [[SingletonTeatheralloc] init];
}
Return shareSingletonTeather;
}
+ (Id) allocWithZone :( struct_NSZone *) zone
{
If (shareSingletonTeather = nil ){
ShareSingletonTeather = [[superallocWithZone: zone] init];
}
Return shareSingletonTeather;
}
-(Id) copyWithZone :( struct_NSZone *) zone
{
Return self;
}
-(NSUInteger) retainCount
{
Return NSIntegerMax;
}
-(Id) retain
{
Return self;
}
-(Onewayvoid) release
{
}
-(Id) autorelease
{
Return self;
}
@ End