4

I'm trying to write an iPhone application, and I have a problem.
I have declared a constant as the app delegate inside a class

#define ikub (iKubMobileAppDelegate *)[[UIApplication sharedApplication] delegate]

And when I need to get the size of an array, which is an instance variable for the application

[ikub.subscriptions count]

I get an error Accessing unknown 'subscriptions' getter method.
I'm not really sure why this is happening.

Please help!!!!

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
Olsi
  • 929
  • 2
  • 12
  • 26
  • 1
    Make sure you have imported the `iKubMobileAppDelegate.h` file into your files that are using your app delegate. And check that your app delegate has a `subscriptions` property. Also, C preprocessor macros tend to be named in `ALL_CAPS` as a convention. – BoltClock Dec 28 '10 at 21:00
  • Checked all of the above and they're all ok. Also, changed the constant to all caps just to be 1000% sure, but I get the same error. :( – Olsi Dec 28 '10 at 21:06

1 Answers1

22

You need to wrap your macro value in parentheses (otherwise, the cast within the macro applies to the property, which at that point is unknown.) So:

#define ikub ((iKubMobileAppDelegate *)[[UIApplication sharedApplication] delegate])
Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • I suggest an inline function instead of a macro. That way you can set breakpoints properly, and avoid macro-specific trickery like the above. Something like static inline iKubMobileAppDelegate* ikub() { return [[UIApplication sharedApplication] delegate]; } – Catfish_Man Dec 28 '10 at 23:19
  • 1
    Careful. `()` in pure C/Objective-C means something different from `(void)`. In this case, he'd want `static inline iKubMobileAppDelegate *ikub(void)`. :) – Jonathan Grynspan Dec 29 '10 at 02:14
  • Great answer but can you elaborate of what cast within macro? – user4951 Oct 08 '12 at 03:24
  • 1
    @JimThio I don't understand your question. – Jonathan Grynspan Oct 08 '12 at 14:59
  • You said the cast within the macro applies to the property. What property? What cast? Which one? Thanks it'll help me to understand. – user4951 Oct 09 '12 at 03:36
  • There's only one cast and only one property used in the macro. So those. – Jonathan Grynspan Oct 09 '12 at 15:05