I've read that you should try to use @class
in your header file instead of #import
but this doesn't work when your @class
contains a delegate protocol that you're trying to use.
MyView.h
#import <UIKit/UIKit.h>
@class MyCustomClass; // <-- doesn't work for MyCustomClassDelegate, used below
@interface MyView : UIView <MyCustomClassDelegate>
@end
I think I'm overlooking something, is there a way to get @class
to work in this situation or is #import
my only choice?
Edit: One work around for this is, of course, declaring your #import MyCustomClass and MyCustomClassDelegate in the private interface section of the .m file instead of the .h file.
You can only forward-declare a protocol in the same header file for usage in method return values or parameter types. In your case you want the class to conform to the protocol, so it won't work since it defines behavior that will be added to the class itself (i.e. the methods it will respond to).
Therefore, you must #import
the protocol. For this reason, it is probably a good idea to split the protocol and class up into separate files. See this answer for more information.