IOS-Singleton mode

Source: Internet
Author: User

1, the concept of a single case

A singleton that indicates that an object of this class can only be instantiated once in an application's declaration cycle, meaning that a class can have only one object.

2, the role of a single case

Because, in an application, a class has only one singleton object, so whether it's A or B or ... , as long as it is instantiated, it is the same object. So we can use this object to pass the value. (For example: This object has n properties, one of which is a rank, and we assign a value of x to the class attribute of the object in a, so wherever it is instantiated, the value of its rank attribute is x, which we can use).

3. How to use

A, new class of inheritance and NSObject:singleclass

b,. h files

    

   

C,. m files

    

D

// The use of singleton classes

singleclass *singleone = [singleclass shareinstance];

Singleone. name = @ " single case A";

NSLog(@ "%@", Singleone. Name);

singleclass *singletwo = [singleclass shareinstance];

NSLog(@ "%@", Singletwo. Name);

Although Singletwo is not assigned, it is also @ " Singleton A".

// print address

NSLog(@ "One address :%@ address :%@", singleone,singletwo); same address

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