Whether it is love or hate, you need a single case. Virtually every iOS or Mac OS app will have at least uiapplication or nsapplication.
What is a single case? Wikipedia is so defined:
in Software engineering, a singleton is a mathematical concept used to implement a single case, and the instantiation of a class is limited to the design pattern of only one object.
Or my understanding is:
Singleton is a class that can only instantiate an object.
Although this is a practical definition of a singleton, it is not necessarily the case in the foundation framework. Like whatNsfilemangerAndNsnotificationcenter,By their class method, respectively.DefaultmanagerAndDefaultcenterGet. Although not strictly a single case, these class methods return a shared instance of a class that can be accessed in all code in the app. We will also use this approach in this article. The best way to use objective-c to implement singleton patterns has been a lot of controversy, and developers (including Apple) seem to change their minds every few years. When Apple introduced Grand Central Dispatch (GCD) (Mac OS 10.6 and iOS4.0), they also introduced a function that was well suited for implementing singleton patterns. The function isdispatch_once:
void Dispatch_once (dispatch_once_t *predicate, dispatch_block_t block);
The function receives adispatch_onceA predicate that checks whether the code block has been dispatched (is a long integer, actually asBOOLUse). It also receives a block of code that wants to be dispatched only once during the lifetime of the application, for this example is used for instantiation of a shared instance.dispatch_onceNot only does it mean that the code will only be run once, but it's also thread-safe, which means you don't need to use things like@synchronizedTo prevent problems that are not synchronized when using multiple threads or queues. Apple's GCD documentation confirms this:
if called by multiple threads, the function synchronizes and so on until the code block is complete.
How do you actually use these? Well, suppose there's aAccountmanagerclass, you want to access the shared instance of the class throughout your app. You can implement a class method simply by following this code:
+ (Accountmanager *) Sharedmanager {
static Accountmanager *sharedaccountmanagerinstance = nil;
static dispatch_once_t predicate; Dispatch_once (&predicate, ^{
Sharedaccountmanagerinstance = [[Self alloc] init];
});
return sharedaccountmanagerinstance;
}
This means that any time you access a shared instance, you need to do just the following:
Accountmanager *accountmanager = [Accountmanager Sharedmanager];
In this case, you now have a shared instance in your app that will only be created once. There are a number of advantages to this approach: 1 Thread safety 2 good for static analyzer requirements 3 and automatic reference count (ARC) compatible 4 requires only a small amount of code the disadvantage of this approach is that it still runs to create a non-common Examples of benefits:
Accountmanager *accountmanager = [[Accountmanager alloc] init];
There are times when you want to have this behavior, but if you're looking for an instantiation of just one instance, you need to be aware of that. Original link: http://objcolumnist.com/2011/07/06/creating-singletons-using-dispatch_once/
IOS uses Dispatch_once to create a single case