Objective-c Single-Case mode

Source: Internet
Author: User

The single pattern of writing objective-c with GCD and C # has a big difference

Declaring h file

#import <Foundation/Foundation.h>@interface me:nsobject<nsobject>@property ( nonatomic) Nsinteger age; +(instancetype) makeme; @end

The difference between Instancetype and ID

Instancetype returns the instance type, and the ID returns an unknown type

①instancetype can return objects of the same type as the method's class, and the ID can only return objects of unknown type, making debugging easier

②instancetype can only be used as a return value, not as a parameter as an ID

The specific principle is to use dispatch_once to create a single instance, because the function runs only once during the program life cycle

void dispatch_once (dispatch_once_t *predicate, dispatch_block_t block);

The first parameter is a predicate that checks whether the block of code represented by the second argument is called, and the second parameter is a block of code that will only be called once throughout the application.

But in the dispath_once version, I think it's a good one down here.

#import "me.h"StaticMe *myself;@implementationMe+(ID) Allocwithzone: (struct_nszone *) zone{Staticdispatch_once_t Onetaken; Dispatch_once (&onetaken,^{Myself=[Super Allocwithzone:zone];    }); returnmyself;}+(instancetype) makeme{Staticdispatch_once_t Onetaken; Dispatch_once (&onetaken, ^{Myself=[[Self alloc]init];    }); returnmyself;}-(ID) copy{returnmyself;}

Here we need to figure out the Allocwithzone method. The initialization of the OC object is alloc and init mates. Alloc partition memory, init configures parameter variables for the instance, as compared to the following code, I removed the Allocwithzone method

#import " me.h " static Me *myself; @implementation me+(instancetype) makeme{    static  dispatch_once_t Onetaken;    Dispatch_once (&onetaken, ^{        myself=[[self alloc]init];    });     return myself;}

The following example is if you use the

Me *myself=[[me Alloc]init];
Me *myself1=[me Makeme];

You will find that you have acquired a new instance of ... Myself and Myself1 are different, this goods is not a single instance ... Of course, because the Alloc+init method we used has bypassed the Dispath_once method, the single-instance has failed.

This means that we need to start from where the instance was initialized.

The introduction to the view Allocwithzone method is

When initializing an instance of a class using the Alloc method, the default is to call the Allocwithzone method. So we can jam this too. Even if the user is using Alloc +init, no new instances will be generated.

Objective-c Single-Case mode

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.