Single Example: Example with afnetworking encapsulation

Source: Internet
Author: User

Ways to create a singleton

1 Import afnetworking 2 3 class Networktools:afhttpsessionmanager {4     5     // Let is thread-safe 6     // use a singleton instance just to get networktools This class takes out this constant attribute to get a single case 7     static Let Shareinstance:networktools = networktools ()89 }

How to use the concrete

1 Import UIKit2 3 classViewcontroller:uiviewcontroller {4 5     Overridefunc viewdidload () {6 super.viewdidload ()7         //The following two singleton addresses are consistent after the compilation is run,8 print (networktools.shareinstance)9 print (networktools.shareinstance)Ten     } One  A     OverrideFunc Touchesbegan (_ Touches:set<uitouch>, withEvent: Uievent?) { -         //after running compilation, the following two singleton addresses are inconsistent, using a singleton must take a constant attribute, and using networking directly creates an instance - print (networktools.shareinstance) the print (Networktools ()) -     } -}

There are two main types of single cases:

1. It's always an example to get through shareinstance.

2. Whatever it is, getting it is always an example.

Single Example: Example with afnetworking encapsulation

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.