I have a class with an delegate property. Anyone who wants to be a delegate must conform to a protocol. I defined everything like this:
#import
I think you need to #import
the header file that defines the protocol. How can the compiler know which methods are available without it?
If you use another class (i.e., as an ivar or as a parameter to a method) then you can use a forward declaration. But if you subclass then you need to #import
.
Put the definition of the protocol in a separate file.
#import that file into any header file that needs it.
Your two classes differ in their use of the protocol. MyClass does not implement the protocol, it has an attibute that is a pointer to a class that implements the protocol. OtherClass should implement the protocol.
OtherClass needs to have available before its interface is defined all the details of the protocols, interfaces and classes that it inherits from. Thus you need the protocol in a header to be #imported in OtherClass.h
MyClass just needs to know the protocol exists in its interface.
Note on Stephen's reply subclassing is the case you can't use forward declarations of classes. (In the example OtherClass is a subclass of NSObject)
Also see Apple's Communicating with Objects, which discusses delegates, protocols, and selectors.
The compiler's warning is correct. OtherClass
doesn't conform to the protocol because it doesn't declare the required fooBarWithFoo
method that the protocol expects.
Have you tried it this way?
#import "MyClass.h"
@interface OtherClass : NSObject <TheDelegateProtocol> {
// ivars
}
- (void)fooBarWithFoo:(CGFloat)foo; // <<< missing bit
@end
He doesn't need to declare the methods to conform to the protocol. He only need to implement them in the .m file.