The difference between "Swfit" swift and OC two grammar writing single cases

Source: Internet
Author: User

The difference between swift and OC two grammar writing single cases

For example, write a single example of a networktools

(1) OC Write a single case

1+(instancetype) sharednetworktools {2     Static IDinstance;3     4     Staticdispatch_once_t Oncetoken;5     6Dispatch_once (&oncetoken, ^{7Instance =[[Self alloc] init];8         //Here you can do some initialization9     });Ten      One     returninstance; A}

(2) Swift writes a single example

1     //to define a private static member2     //' Let ' is thread-safe3     //This code is lazy to load, and will not run until the first call.4     Private StaticLet instance =Networktools ()5     6     classFunc Sharednetworktools ()Networktools {7         returninstance8}

If you want to initialize some properties beforehand, you can write this

1  Private StaticLet Instance:networktools = {2Let Networktool =Networktools ()3           //This initializes the properties4         5            returnNetworktool6     }()7     8     classFunc Sharednetworktools ()Networktools {9         returninstanceTen}

The difference between "Swfit" swift and OC two grammar writing single cases

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.