开发者

Method prototypes in implementation file for pseudo-private methods?

开发者 https://www.devze.com 2023-03-28 19:22 出处:网络
I have some methods that I don\'t declare in my class\'s interface because only code within the class should use them. But the arrangement of my methods generates some \"... might not respond to selec

I have some methods that I don't declare in my class's interface because only code within the class should use them. But the arrangement of my methods generates some "... might not respond to selector ..." warnings when methods want to call methods that are imple开发者_运维知识库mented below them.

Is there any way I can declare prototypes for these pseudo-private methods in my implementation file so that I don't get these warnings?


You can use a class extension. I prefer them over categories (for this purpose) because the methods they declare must be implemented in the main @implementation block for the corresponding class.

It is common for a class to have a publicly declared API and to then have additional methods declared privately for use solely by the class or the framework within which the class resides. You can declare such methods in a category (or in more than one category) in a private header file or implementation file as mentioned above. This works, but the compiler cannot verify that all declared methods are implemented.

Class extensions allow you to declare additional required methods for a class in locations other than within the primary class @interface block

You declare a class extension like this:

@interface MyObject () // No name is given in the parentheses


Just use a category - I do something like this in my .m files:

@interface MyClass (PrivateMethods)
- (void)privateMethod1;
- (void)privateMethod2:(NSString *)aParam;
@end

@implementation MyClass
- (void)privateMethod1;
- (void)privateMethod2:(NSString *)aParam;
@end


Private methods don't exist in objective-c. See this post.

Best way to define private methods for a class in Objective-C

0

精彩评论

暂无评论...
验证码 换一张
取 消