iOS single case

Source: Internet
Author: User

#import <Foundation/Foundation.h> @interface fucardsettings:nsobject+ (fucardsettings *) sharedinstance; @end
#import   "FuCardSettings.h" @implementation  FuCardSettings//=================  single Case  =========== === startstatic fucardsettings *sharedsettings = nil;+  (FuCardSettings *) Sharedinstance {        if (sharedsettings  ==   nil) {            sharedsettings =  [[[self alloc] init]autorelease];          }     return  sharedsettings;} +  (ID) Allocwithzone: (nszone *) zone {     @synchronized (self)  {         if  (Sharedsettings == nil)  {             sharedsettings = [super allocwithzone: Zone];        }    }&nBsp;   return sharedsettings;} +  (ID) Copywithzone: (nszone *) zone {    return self;} -  (ID) retain {    return self;} -  (Nsuinteger) Retaincount {    return nsuintegermax;} -  (oneway void) release {}-  (ID) autorelease {    return self;} =================  Single Case  ============== end@end

The above is a traditional single case:

There is also a single example that is implemented with GCD:

 #import  <foundation/foundation.h>@ Interface tool1 : nsobject@property (nonatomic,copy) nsstring *test;+  (instancetype) instance, @end @implementation tool1+  (instancetype) instance{    static tool1 * tool;    static dispatch_once_t oncetoken;     Dispatch_once (&oncetoken, ^{        tool = [[tool1  alloc] init];            });     return tool;} -  (instancetype) init{    self = [super init];     if  (self)  {        _test = @ "test";     }    return self;} @end 

The notation has the following characteristics:

1. Thread safety.

2. Meet the requirements of the static analyzer.

3. Compatible with ARC







iOS single case

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.