Recommended wording for iOS singleton mode (reprint)

Source: Internet
Author: User

1+(instancetype) shareduserloginmodel{2  3     StaticYlwuserloginmodel *instance;4     5     Staticdispatch_once_t Oncetoken;6     7Dispatch_once (&oncetoken, ^{8         9Instance =[[Ylwuserloginmodel alloc]init];Ten          One     }); A     returninstance; -      -}

The benefits of this notation are:

1. Thread safety.

2. Meet the requirements of the static analyzer.

3. Compatible with ARC

Summary: 1. This method can be used to create a singleton or some initialization action, to ensure its uniqueness. 2. This method is thread-safe, so be sure to use it boldly in a child thread. (If your dispatch_once_t *predicate object must be a global or static object.) This is important, and if this is not guaranteed, there is no guarantee that the method will only be executed once. )

Transferred from: http://www.lrdup.net/archives/1034

Recommended wording for iOS singleton mode (reprint)

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.