IOS Single case

Source: Internet
Author: User

In iOS development, a singleton is one of the most useful design patterns. It is one of the most useful ways to share data between code without manually passing parameters. For a single case and other design patterns, refer to this book:

"Cocoa Design Pattern"

Background

The singleton is an important concept, it is an extremely convenient design pattern. The concept of Singleton is used extensively in the iphone SDK, for example, UIApplication's Sharedapplication method, which returns a uiapplication instance of the current application at any time.

How it will be implemented

Use the following code to implement a singleton class:

MyManager.h

#import <foundation/Foundation.h>

@interfaceMyManager: nsobject {

NSString *someproperty;

}

@property (nonatomic, retain) NSString *someproperty;

+ (ID) Sharedmanager;

@end

Mymanager.m

#import "MyManager.h"

@implementationMyManager

@synthesizesomeProperty;

#pragmamark Singleton Methods

+ (ID) Sharedmanager {

static Mymanager*sharedmymanager = nil;

Static Dispatch_once_toncetoken;

Dispatch_once (&oncetoken, ^{

Sharedmymanager = [[Self alloc] init];

});

return sharedmymanager;

}

-(ID) init {

if (self = [super init]) {

Someproperty =[[nsstring alloc] initwithstring:@ "Default property Value"];

}

return self;

}

-(void) Dealloc {

Should never is called, but justhere for clarity really.

}

@end

We define a static variable called Sharedmymanager, which is only instantiated once in the Sharedmanager method. With GCD's Dispath_once method, we ensure that the Sharedmymanager method is only created once. This is thread-safe and you don't have to worry about anything.

However, if you do not want to use GCG, you can also implement the Sharedmanager method as follows:

Non--GCD code

+ (ID) Sharedmanager {

@synchronized (self) {

if (Sharedmymanager = = nil)

Sharedmymanager = [[Self alloc] init];

}

Returnsharedmymanager;

}

This invokes the Singleton object:

Mymanager *sharedmanager = [Mymanager Sharedmanager];

In my code, this code is used in many places. I use these singleton objects to handle corelocation or CoreData.

Non-ARC code

If you do not use arc (not recommended), you should use the following code:

MyManager.h (non-ARC)

#import "MyManager.h"

static Mymanager *sharedmymanager = nil;

@implementation Mymanager

@synthesize Someproperty;

#pragma Mark Singleton Methods

+ (ID) Sharedmanager {

@synchronized (self) {

if (Sharedmymanager = = nil)

Sharedmymanager = [[Super Allocwithzone:null] init];

}

return sharedmymanager;

}

+ (ID) Allocwithzone: (Nszone *) Zone {

return [[Self sharedmanager]retain];

}

-(ID) Copywithzone: (Nszone *) Zone {

return self;

}

-(ID) retain {

return self;

}

-(unsigned) Retaincount {

return Uint_max; Denotes anobject that cannot be released

}

-(OneWay void) Release {

Never release

}

-(ID) autorelease {

return self;

}

-(ID) init {

if (self = [super init]) {

Someproperty = [[NSString alloc] initwithstring:@ "Default propertyvalue"];

}

return self;

}

-(void) Dealloc {

Should never is called,but just here for clarity really.

[Someproperty release];

[Super Dealloc];

}

@end

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.