0

I read a post to use macro to reference AppDelegate. I wonder what I should use to replace (MyOwnAppDelegate*)

#define AppDelegate ((MyOwnAppDelegate*)[[UIApplication sharedApplication] delegate])
Community
  • 1
  • 1
Philip007
  • 3,190
  • 7
  • 46
  • 71
  • 2
    If you have to reference AppDelegate somewhere else in your code - you're most probably doing something wrong. – Eimantas Sep 13 '12 at 14:35
  • Why would it be wrong to do that? It's certainly not a good practice to reference it from every other class ([spaghetti code](http://en.wikipedia.org/wiki/Spaghetti_code)). But you can't avoid it in every case. – DrummerB Sep 13 '12 at 14:40
  • Can you describe a case where you shouldn't avoid it? The only things that come to mind are really wacky (like you need the main `UIWindow`, but you're not in the view hierarchy yet). Other than that, the application delegate shouldn't be holding much that anyone else cares about. – Rob Napier Sep 13 '12 at 14:54
  • I need my data to be global. That's why I store it in AppDelegate. Otherwise I need to create another singleton class. Is there a better way? – Philip007 Sep 14 '12 at 01:42
  • Typically you should create a separate model class or classes that hold your data. You can either hold you model in a singleton (`PersonManager` or even just `Model`), or you can create your top-level model in the app delegate and hand it to the first view controller. This is how document-based apps work, but is fine for all kinds of apps. There is no problem with creating another singleton, but the app delegate isn't the right one to use. It makes the code very hard to reuse. – Rob Napier Sep 14 '12 at 02:57
  • "or you can create your top-level model in the app delegate and hand it to the first view controller". What does it mean exactly? Sorry I couldn't follow. – Philip007 Sep 14 '12 at 03:07

2 Answers2

3

If you're referencing the UIApplicationDelegate so often that you need a special macro for it, then you're referencing it too often. The application delegate is the delegate of the main UIApplication object. It is not a general place to store "stuff." See the following discussions:

Community
  • 1
  • 1
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
2

If it's a more recently started project, your delegate is probably called AppDelegate. To find out, you have to find the class in your project that implements NSApplicationDelegate or UIApplicationDelegate (depending on your target platform).

enter image description here

DrummerB
  • 39,814
  • 12
  • 105
  • 142