Private Method Declarations
A problem arises when you’d like to declare a method in such a way that all other methods in the same class can see the declaration (and can thus call the method) without putting that declaration in the class’s interface section where every other class that imports the header file will also be able to see and call it.
The solution is to put an interface section for a category on your own class in the implementation file, which no one imports (Example 10-1).
Example 10-1. Declaring a method privately
// [in MyClass.m]
#import "MyClass.h"
@interface MyClass (Tricky)
- (void) myMethod;
@end
@implementation MyClass
// all methods here can call myMethod
@end
This trick cannot completely prevent some other class from calling this class’s myMethod — Objective-C is too dynamic for that — but at least a normal call to myMethod from some other class will get its hand slapped by the compiler.
In Example 10-1, the compiler will not warn if the methods declared in the category interface section (such as myMethod) are not defined in the implementation section. If this worries you, there are two solutions. One is to provide a named category implementation section, corresponding to the named category interface section; if you fail to implement the category-declared methods in this category implementation section, the compiler will warn:
@implementation MyClass (Tricky)
// must implement myMethod here, or compiler will warn
@end
The other approach is just the opposite, namely to remove the category name altogether from the category implementation section; if you then fail to implement the categorydeclared methods in the normal implementation section, the compiler will warn:
@interface MyClass ()
- (void) myMethod;
@end
This nameless type of category is called a class extension.
Protocols
A protocol is just a named list of method declarations, with no implementation. A class may formally declare that it conforms to (or adopts) a protocol; such conformance is inherited by subclasses. This declaration satisfies the compiler when you try to send a corresponding message.
A protocol method may be required or optional.
A protocol is just a named list of method declarations, with no implementation. A class may formally declare that it conforms to (or adopts) a protocol; such conformance is inherited by subclasses.
Here’s how the NSCopying protocol is defined (in NSObject.h, where your code can see it):
@protocol NSCopying
- (id)copyWithZone:(NSZone *)zone;
@end
That’s all there is to defining a protocol. The definition uses the @protocol compiler directive; it states the name of the protocol; it consists entirely of method declarations; and it is terminated by the @end compiler directive. A protocol definition will typically appear in a header file, so that classes that need to know about it (in order to call its methods) can import it. A @protocol section of a header file is not inside any other section (such as an @interface section).
Conform formally to the NSCopying protocol
@interface MyClass : NSObject <NSCopying>
Example of how Cocoa uses protocols
@property (nonatomic, assign) id<UITableViewDataSource> dataSource
Checking if object responds to some method, you can call respondsToSelector: but it slows things down a little.