Private methods in Objective-C
Private method in Objective-C
Objective-C doesn't have the concept of private methods although you can use @private, @public, @protected to specify visibility scope for variables of a class.
One way to simulate private methods is to use category.
// in MyClass.m, before the main @implementation block
@interface MyClass (Private)
- (void)privateMethod;
@end
// you won't get warnings if this block is missing
@implementation MyClass (Private)
- (void)privateMethod {
//do sth...
}
@end
Note that it's a good practice to write an @implementation block for the category as soon as you finish the category @interface declaration. If the @implementation block for the category is missing, the compiler won't warn you about any missing method implementations.
// in MyClass.m, before the main @implementation block
@interface MyClass ()
- (void)privateMethod;
@end
@implementation MyClass
- (void)privateMethod {
//do sth...
}
@end
You can also leave the category name blank to use an extension. If you do so, you must implement the methods in the main @implementation block. The compiler will always issue warnings when implementations for the methods are missing.
References
0 comments:
Post a Comment