IOS-3-Objective-C language: Singleton Design Model

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.