IOS Singleton mode

Source: Internet
Author: User
The singleton patterns of all programming languages are similar. Object-c, Java, C + +, and so on, with the language does not have much to do with the grammar, only a little relationship. In iOS if you have a class: Accountmanager, you want to define a singleton then the footwork is as follows: one. In the. h file there should be a definition similar to the following: + (ID) sharedinstance; Two. Within the. m file the class should have the following definition://declares a globally unique static object, also Accountmanager type static Accountmanager * _sharedinstance;//method implementation + (ID) sharedinstance {@synchronized ([Accountmanagerclass]) {if (_sharedinstance = = nil) {_sharedinstance = [[ ACCOUNTMANAGERALLOC] init];}} Return_sharedinstance;} Three. If you want to use the singleton in another class object and call a method of the singleton (todosomething): [[Accountmanager sharedinstance] todosomething]; additional instructions: 1. Sharedinstance This name is what I used to use, you can use another name, any, in short, to keep accountmanager This class can only have one instance 2 during the entire application run. The example here gives you a "lazy" singleton, as well as an additional singleton to complete the task, such as "A Hungry Man type" singleton. If you don't know what a lazy and a hungry man you can find on Google

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.