Do you lose Static Typing ability while using a #DEFINE statement?
-
27-06-2021 - |
Question
I seem to be losing static typing ability when defining,
#define myAppDelegate (WorkClockAppDelegate *) [[UIApplication sharedApplication] delegate]
To do this,
[myAppDelegate doSomething];
Xcode doesn't know what doSomething is, although it will compile and run fine as long as I know what I'm doing, and doSomething actually exists on myAppDelegate!
Is there any way I can overcome this?
Solution
You don't lose it, no -- you are probably omitting an #import
, or the order of #import
s may be wrong.
Is there any way I can overcome this?
Just use a function:
WorkClockAppDelegate * myAppDelegate() {
// TODO add error checking
return (WorkClockAppDelegate *) [[UIApplication sharedApplication] delegate];
}
or a method:
+ (WorkClockAppDelegate *)workClockAppDelegate {
// TODO add error checking
return (WorkClockAppDelegate *) [[UIApplication sharedApplication] delegate];
}
to introduce type safety.
although you could use an extern
variable, that's not very safe.
using #define
is not a good solution, imo.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow