Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
Just a pro tip: If you've got a whole bunch of view controllers in your app that share an object, you might want to consider creating a singleton instance. A singleton is just an object that can be used by any other object in your app. If you ever called [UIApplication sharedApplication], you used a singleton.
 
You are off on your explanation of a singleton. Any object can be used by any other number of objects, so that isn't what makes it special.

The special trait is that when instantiating an object from such a class, you only ever get one instance. The class does not allow more than one object to be created. This allows for one global instance that can be shard by any other objects that need access to it and insures that you don't accidentally create more than one of these shared objects.

Here is Apples explanation: https://developer.apple.com/library.../Conceptual/DevPedia-CocoaCore/Singleton.html
 
I was under the impression that the value of the singleton, at least in this usage, was the scope (global access) of the instance.

Please correct me where I'm wrong. Having access to a global instance, make the transfer of data very simple. Much like user settings, you can pass them thru to every method that needs them, or you can have global access to them.

This could be balanced against the option of having a datastore where each method does a lookup to find the needed data and the overhead of having to do a lookup and accessing data from a datastore vs having a global structure that stores the same.

Just like user settings or any other datastore, the singleton has one instance only.

For some apps, this could become an issue of memory usage vs the time it take to access a datastore, in addition to the need to update the same.
 
I was under the impression that the value of the singleton, at least in this usage, was the scope (global access) of the instance.

Please correct me where I'm wrong. Having access to a global instance, make the transfer of data very simple. Much like user settings, you can pass them thru to every method that needs them, or you can have global access to them.

This could be balanced against the option of having a datastore where each method does a lookup to find the needed data and the overhead of having to do a lookup and accessing data from a datastore vs having a global structure that stores the same.

Just like user settings or any other datastore, the singleton has one instance only.

For some apps, this could become an issue of memory usage vs the time it take to access a datastore, in addition to the need to update the same.

Yes. What I like is that I can access the same information from all my view controllers without explicitly passing it on from one controller to the next.
 
I read a tutorial about just passing data from one to another and it seemed very cumbersome. One problem I saw was that it was all done based on fixed data, meaning that if you change the data structure, you have to go in and change the code.

Decades ago, I wrote a generic data browser that would read the structure and display whatever it was. It seems pretty backwards that the tutorial was so fixed and cumbersome given the whole MVC/MVVC model and how things are supposed to be flexible, yet it's dependent on the data structure being fixed.

Although ObjC/Swift isn't really a database language.
 
Saw what looks like a good post about this:
There are various ways by which a data can be received to a different class in iOS. For example -

  1. Direct initialization after the allocation of another class.
  2. Delegation - for passing data back
  3. Notification - for broadcasting data to multiple classes at a single time
  4. Saving in NSUserDefaults - for accessing it later
  5. Singleton classes
  6. Databases and other storage mechanisms like plist, etc.

http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.