How can I create a singleton class in Objective C?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Okay appDev, you will probably find quite a few different techniques to do this on the web. However, for iOS app development, I think the most convenient way is to do the following:
Write your method(s) for getting the singleton object. (Recommendation: use
dispatch_oncethread and GCD for this).Wrap your method(s) in a macro and add it to your
$Project$-Prefix.pchfile.Call the one line macro whenever you need singleton object for a class.
Example:
CommonMacros.h:
YourProject-Prefix.pch:
YourSingletonClass.m: