1

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 implemented 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?

Randall
  • 2,083
  • 2
  • 18
  • 20
  • possible duplicate of [Best way to define private methods for a class in Objective-C](http://stackoverflow.com/questions/172598/best-way-to-define-private-methods-for-a-class-in-objective-c) – mmmmmm Jan 08 '12 at 20:52

3 Answers3

7

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
Community
  • 1
  • 1
albertamg
  • 28,492
  • 6
  • 64
  • 71
1

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
Tim Dean
  • 8,253
  • 2
  • 32
  • 59
  • I can't think of any reason you'd do this over a class extension. – Chuck Aug 15 '11 at 19:40
  • It's the same thing but defined in the .m file and thus visible only to the other methods defined in that file. – TechZen Aug 15 '11 at 19:42
  • @Chuck - You're probably right. I think I do it this way mostly out of habit and I've never found the need for the need for the extra compiler checks you get with extensions. – Tim Dean Aug 15 '11 at 19:48
0

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

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

Community
  • 1
  • 1
Trevor
  • 11,269
  • 2
  • 33
  • 40