Cocoa singleton and shared instances

Why doesn't Apple have classes like pure singleton, for example, although the UIApplication class gives you access to the UIApplication singleton, for example:

UIApplication *sharedApplication = [UIApplication sharedApplication];

Nothing prevents you from explicitly creating an instance of UIApplication, for example:

UIApplication *newApplication = [[UIApplication alloc] init];

The result, however, is an exception. The exception clearly states that only one instance of the UIApplication class can be available at any time.

Then why not get a clean singleton by returning the same instance to the default initializer?

I ask this question in order to have clarity when creating a singleton in my project in order to keep track of which way is better.

+4
source share
2 answers

, : , Apple , , Apple.

[...],

, Swift :

class SomeManager {
  static let sharedInstance = SomeManager()
}

Objective-C ( ) , .

, . .

BTW: . , .

+1

, Apple , , , .

, Apple Singletons . , , , UIApplication, UIApplication.

UIApplication . .

, , , , , . Apple .

, ! Apple , ! , , !

, . , .

Apple - , . , . , -, Apple .

, , , , . !

,

0

Source: https://habr.com/ru/post/1648463/


All Articles