Summary: The singleton design pattern requires overriding the methods of the parent class. Because global instances do not allow deallocation, the Retain,release,autorelease methods associated with memory management need to be rewritten to avoid the memory aspects of the instance and to prevent the reference count from changing.
The specific instance code of the singleton pattern is as follows:
SingletonTeather.h file
#import <Foundation/Foundation.h>
@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"
Static Singletonteather *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
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Ios-3-objective-c Language: Single-case design mode