Single-Case mode

Source: Internet
Author: User

When you want to have only one object for a class in an application, you can consider using singleton mode, which is generated by NSObject alloc, and need to ensure the uniqueness of the object.

The. h file

+ (instancetype) Sharedplayer;

In the. m file

+ (instancetype) sharedplayer{    static Playermanager *manager;   //A static instance object of a singleton class, because the object needs uniqueness, it can only be static type

Static dispatch_once_t once;

Dispatch_once (&once, ^{ //Singleton mode is the only interface to the external, the use of the Dispatch_once function in an application will only be executed once, and dispatch_once to ensure thread safety

Manager = [[Playermanager alloc] init];


Return manager;


Single-Case mode

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: 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.