문제

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?

도움이 되었습니까?

해결책

You don't lose it, no -- you are probably omitting an #import, or the order of #imports 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top