How to make a forward declaration for a private method?

I put my methods into groups using the #pragma sign in the implementation. But sometimes the code for the implementation of the method appears under the code that calls this method, and I get warnings "Instance method not found". This happens when I use private methods. How to fix it?

+5
source share
3 answers

The easiest way is to use an anonymous category. Add something like this to the top of your file .mbefore @implementation:

@interface MyClass()
- (void)myPrivateMethod;
@end
+8
source

, . , "" API - , - .

YourClass.m

@interface MyClass()

- (void)myPrivateMethod;

@end


@implementation MyClass

- (void)myPublicMethod
{
    // This will not throw an error or warning
    [self myPrivateMethod];
} 

- (void)myPrivateMethod
{
    // Do something
}

@end
+1

In your Class.m implementation file, you can add an interface section at the beginning and declare private functions there:


@interface YourClassName (private)

-(void)aPrivateMethod:(NSString*)aParameter;
...

@end

@implementation YourClassName
...
@end

+1
source

All Articles