开发者

how to make a single method in a class to private in Objective-c?

how to make a sin开发者_开发百科gle method in a class to private in Objective-c?


There's no concept of private methods in Objective-C. What you can do, however, is obmitting the method in the header file and adding a category in a private file.

Usually this is down like this:

@interface MyObject : NSObject {
   // ivars
}
// public methods
@end

// usually in a seperate file
@interface MyObject ()
// private methods
@end

An empty name for a category means that it's a class extension, this tells the compiler that the method is required in the main implementation block.


You can't strictly speaking create private methods in Objective-C; but you can create a category in the file containg the implementation (MyClass.m), hiding it from the header file (MyClass.h).

//MyClass.h    
#import <Foundation/Foundation.h>

@interface MyClass : NSObject {

}

@end

Implementation file:

//MyClass.m

@interface MyClass ()

- (void)myPrivateMethod;

@end

@implementation MyClass

- (void)myPrivateMethod {
  return;
}

@end


Objective-C doesn't support private methods natively by design.

However, you can achieve private method control by using "class extensions" (anonymous categories).

The basic idea is to declare a class extension with your private methods inside the implementation file for your class (not its header), but outside of your @implementation block. You can then implement the methods and no outside class will be able to see them. Like this:

@interface MyClass ()
    - (void)myPrivateMethod;
@end

@implementation MyClass
    - (void)myPrivateMethod
    {
        //implementation goes here
    }

    -(void)someOtherMethod
    {
        [self myPrivateMethod];
    }
@end

Using a class extension forces you to implement the methods in the main @implementation block and requires that they are implemented (much like as if they were declared in the main @interface block in the header).

Unfortunately, due to Objective-C's dynamic nature, this won't actually prevent those methods from being called by other classes. They will get a warning telling them that the class may not respond to the message, but at runtime it will work. The only thing this gives you is the ability to hide the methods from other programmers.


You can't. Only instance variables can be marked as private.

Though there are ways to do something equivalent to private methods. The general idea is that you don't declare the private method in your header but in your implementation file.

Therefore, if you'd want to have a public (myPublicMethod) and private (myPrivateMethod) method, you're header could look like this:

@interface MyClass {
}
- (void)myPublicMethod;
@end

And then you have three options for your implementation file:

1.

Don't declare the method in any @interface section and just make sure it's implemented before it's being used in the @implementation section.

@implementation MyClass

- (void)myPrivateMethod {  }

- (void)myPublicMethod {
    [self myPrivateMethod];
}

@end

2.

Use an anonymous category in your implementation file to declare the private methods and implement them in the main @implementation block.

@interface MyClass ()
- (void)myPrivateMethod;
@end

@implementation MyClass

- (void)myPrivateMethod {  }

- (void)myPublicMethod {
    [self myPrivateMethod];
}

@end

3.

Use a regular category in your implementation file and implement the private methods in a separate @implementation block.

@interface MyClass (PrivateMethods)
- (void)myPrivateMethod;
@end

@implementation MyClass (PrivateMethods)

- (void)myPrivateMethod {  }

@end

@implementation MyClass

- (void)myPublicMethod {
    [self myPrivateMethod];
}

@end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜