views:

208

answers:

1

I'm building a custom Xcode framework, and I have a class called AXController that has a class method called showActivationWindow. showActivationWindow initializes and shows a window using AXWindowController which is a subclass of NSWindowController. Then, AXWindowController calls activate which is a class method in AXController on a button press in the window it displays.

I included AXWindowController.h in AXController.h and included AXController.h in AXWindowController.h so each could access the other's methods, but this is throwing a lot of Xcode warnings. It also seems like a very inelegant solution.

What is the correct way to solve this problem?

+3  A: 

It's not a good idea to import header files recursively. There's a directive @class (link to Apple doc) which tells that there is a class named as such. In practice the usage is something like A.h

@class B;
@interface A:NSObject {
   B* anInstanceOfB;
} 
...
@end

and B.h

#import "A.h"
@interface B:NSObject {
  A* anInstanceOfA;
}
...
@end

Then you can import A.h and B.h as you like from your .m file! But be careful not to make a retain cycle, if you don't use garbage collection.

Yuji
Thanks for your answer. I changed my code to use `@class`. But I'm still getting warnings involving the following: In class A, I have `static NSString *appName;` outside of the `@interface`, and now I'm getting a warning that says "`appName` defined but not used." What does this warning mean and why am I getting it?
Chetan
You may want to learn how to declare global variables in C. This link http://stackoverflow.com/questions/496448/how-to-correctly-use-the-extern-keword-in-c might be helpful. In a nutshell, in `A.h`, write `extern NSString*appName;`In `A.m`, write `#import "A.h"` and then `NSString*appName;`Finally, if you want to refer to appName from `B.h`, just do `#import "A.h"` inside `B.h`. I guess you want to have a class variable like C++, but `static` in C or Objective-C is something different, and it works differently.
Yuji
Actually, after some researching, the correct way to declare class-level variables is as follows: `static NSString *myString` in A.m.Reference: http://stackoverflow.com/questions/1063229/objective-c-static-class-level-variables
Chetan