How and where should I store instances of objects that are required globally in my iOS app?

I am creating an iOS application. Most applications require access to a persistent object. This object is created when the application is loaded through the application delegate.

I have a problem with numerous View controllers that need access to this object.

What is the best way and best practice for creating global objects that can be accessed from anywhere in the application?

Examples will be appreciated. Many thanks.

+3
source share
1 answer

You might want to look at Singleton . The related article contains a pretty good description, including how to implement it in Cocoa.

If Singleton doesn't make sense in your context, and you still need a global reference to your variable, you can simply put the link to it in your AppDelegate . ( Not recommended )

Access to it can be obtained from the application at any time using:

[UIApplication sharedApplication] delegate]

+6
source

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


All Articles