2

I have an some MyData class. MyData depends on AppDelegate. (I have a MyData object in AppDelegate)

I would like to use MyData in MainViewController and for this, I'm ashamed to import AppDelegate inside MainViewController. I'm a beginner developer, and not an iOS one.

In MainViewController, how do I get that MyData (initialized in AppDelegate), without importing Appdelegate?

  • 3
    Hi – why is `MyData` owned by the app delegate? It sounds like a [model](https://en.wikipedia.org/wiki/Model–view–controller#Components) of the model view controller, in which case it should probably be owned by someone else, or perhaps a [singleton](https://en.wikipedia.org/wiki/Singleton_pattern). In general, I'd suggest keeping the App delegate very light weight. – Benjohn Feb 18 '16 at 08:20
  • @Benjohn thanks, that's interesting, I will think about it. it's just that MyData depends also on an sdk initialised in AppDelegate. –  Feb 18 '16 at 08:28
  • 1
    i'm agreed with the @Benjhon – Ratul Sharker Feb 18 '16 at 08:42
  • Ah – it is likely that you can (and should) avoid initialising the API in the delegate. You might initialise it lazily when first requested for use by another component. Or you could use the [`initialize` or `load`](http://stackoverflow.com/questions/13326435/nsobject-load-and-initialize-what-do-they-do) methods. Or one of the application notifications such as [`NSApplicationDidBecomeActiveNotification`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSApplication_Class/#//apple_ref/c/data/NSApplicationDidBecomeActiveNotification). – Benjohn Feb 18 '16 at 09:31
  • In some cases it is a pragmatic solution to initialise in the App delegate. When you do this, its still best if the delegate doesn't "own" the model. It should obtain the model instance (perhaps with `[Model sharedInstance]`) and then initialise it. – Benjohn Feb 18 '16 at 09:36
  • @Benjohn hi, thank you, I'll work on it, but what do u think about user2529173's answer? –  Feb 18 '16 at 10:20
  • It will work, but it will not scale. What I mean by this is: it gets the job done today, but tomorrow's job will be to maintain and extend this software. The solution given is a poor starting point for tomorrow's work. I think that you would do well to look at some examples of well put together applications and look for some tutorials that teach you how to structure applications. I don't have any useful links in that regard though, sorry. – Benjohn Feb 18 '16 at 12:12

2 Answers2

1

Make your MyData class as a singleton. The Singleton's purpose is to control object creation, limiting the number of objects to one only. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields.

initialise the myData instance in appdelegate and update MyData stored properties if you want to set some value to its properties. In mainviewcontroller you can import MyData and access the mydata properties.

Prabhu.Somasundaram
  • 1,380
  • 10
  • 13
0

I've used this in my code, it should work for you too

    let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let someString = delegate.someString

And in the AppDelegate:

  var someString:String = "asdf"
user2529173
  • 1,884
  • 6
  • 30
  • 43