What is SharedInstance actually?

前端 未结 4 1813
礼貌的吻别
礼貌的吻别 2020-12-17 01:49

What is sharedInstance actually? I mean what is the usage?

Currently I\'m having some problem in communicating between 2 different files.

Here\'s my question:<

4条回答
  •  醉酒成梦
    2020-12-17 02:48

    A sharedInstance is often implemented with the singleton pattern. Like in [UIApplication sharedApplication] -> There is only one application which you access through this singleton.

    The idea is to have one instance of a class which can be accessed by calling a class method, in objective-c commonly named sharedXXX.

    To solve your problem you could actually implement a singleton of a model class and write and access date to and from one existing instance which can be accessed with a static method, let's call it sharedModel.

    The next step to improve on your model and updating the views would be KVO: Key Value Observing. You add an observer in your viewController to 'watch' changes made to your model. If such a change occurs your viewController is informed and you can update the view then.

    You can read more about KVO in Apple's documentation or over at mindsizzlers for a small and easy tutorial.

提交回复
热议问题