是否可以在 Objective-C 中将方法声明为私有方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/647079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to declare a method as private in Objective-C?
提问by Tommy
Is it possible to declare a method as private in Objective-C?
是否可以在 Objective-C 中将方法声明为私有方法?
回答by Barry Wark
If you're working in Objective-C 2.0, the best way to create methods that are "hard" for others to call is to put them in a class extension. Assuming you have
如果您正在使用 Objective-C 2.0,那么创建对其他人“难以”调用的方法的最佳方法是将它们放在类扩展中。假设你有
@interface MyClass : NSObject {
}
- (id)aPublicMethod;
@end
in a MyClass.hfile, you can add to your MyClass.mthe following:
在MyClass.h文件中,您可以添加MyClass.m以下内容:
@interface MyClass () //note the empty category name
- (id)aPrivateMethod;
@end
@implementation MyClass
- (id)aPublicMethod {...}
- (id)aPrivateMethod {...} //extension method implemented in class implementation block
@end
The advanage of a class extension is that the "extension" methods are implemented in the original class body. Thus, you don't have to worry about which @implementationblock a method implementation is in and the compiler will give a warning if the extension method is not implemented in the class' @implementation.
类扩展的优点是“扩展”方法在原始类主体中实现。因此,您不必担心@implementation方法实现在哪个块中,如果扩展方法未在类中实现,编译器将发出警告' @implementation。
As others have pointed out, the Objective-C runtime will not enforce the privateness of your methods (and its not too hard to find out what those methods are using class dump, even without the source code), but the compiler will generate a warning if someone tries to call them. In general, the ObjC community takes a "I told you not to call this method [by putting it in a private class extension or category or just by documenting that the method is private] and you called it anyways. Whatever mess ensues is your fault. Don't be stupid." attitude to this issue.
正如其他人指出的那样,Objective-C 运行时不会强制您的方法的私有性(即使没有源代码,也不难找出这些方法正在使用什么类转储),但编译器会生成警告如果有人试图给他们打电话。一般来说,ObjC 社区认为“我告诉过你不要调用这个方法 [通过将它放在私有类扩展或类别中,或者只是通过记录该方法是私有的] 并且你无论如何都调用它。无论发生什么混乱都是你的错“别傻了。” 对这个问题的态度。
回答by Chuck
No, any object can send any message to any other object. You can, however, put the method in a category that's part of the class's implementation file. That way, you'll get a "Class may not implement this method" warning if you try to call it anywhere else. That's the normal way of making a method "private."
不,任何对象都可以向任何其他对象发送任何消息。但是,您可以将该方法放在属于类实现文件的类别中。这样,如果您尝试在其他任何地方调用它,您将收到“类可能无法实现此方法”的警告。这是使方法“私有”的正常方式。
回答by Andrew Grant
There is nothing that will prevent the method being called (since objective-c is message based anything can be sent any message), but you can declare them outside of the header so they are not visible and the compiler will generate warnings if used.
没有什么可以阻止调用该方法(因为objective-c 是基于消息的,任何消息都可以发送任何消息),但是您可以在标头之外声明它们,因此它们不可见,并且编译器将在使用时生成警告。
This works for both class and instance methods.
这适用于类和实例方法。
E.g.
例如
#import "SomeClass.h"
// Interface for hidden methods
@interface SomeClass (hidden)
+(void) hiddenClassMethod;
-(void) hiddenInstanceMethod;
@end
Note: Do NOT declare variables like this or they will become class-variables - e.g. only one variable will be used by all instances.
注意:不要像这样声明变量,否则它们将成为类变量——例如,所有实例只会使用一个变量。
回答by Abizern
You can do so by using categories. I've got a fuller description in my answer to thisSO question.
您可以通过使用类别来做到这一点。我在对这个SO 问题的回答中有更完整的描述。
As has been said, you can't stop anyone sending a message to a selector, but by using categories you can reduce the visibility of these functions.
如前所述,您无法阻止任何人向选择器发送消息,但是通过使用类别,您可以降低这些功能的可见性。
Also, you can have more than one category extending a class. So, by using informative category names you can group private functions into related blocks, improving the self-documenting nature of your code.
此外,您可以有多个类别来扩展一个类。因此,通过使用信息丰富的类别名称,您可以将私有函数分组到相关块中,从而改进代码的自文档化特性。
回答by vasi
As others mentioned, you can't have code that's
正如其他人提到的,你不能拥有这样的代码
- a method, and
- impossible to call from outside a class.
- 一种方法,以及
- 不可能从课外打电话。
Folks have already pointed out that you can abandon point 2, and get a method that's hard-but-not-impossible to call. Alternatively, why not abandon point 1?
人们已经指出,您可以放弃第 2 点,并获得一个很难但并非不可能调用的方法。或者,为什么不放弃第 1 点?
static id myPrivateMethod(MyObject *me, int arg1, id arg2) { ... }
static id myPrivateMethod(MyObject *me, int arg1, id arg2) { ... }
Now the code can only be called from within same file. You don't get any of the magic private-member access you can get with a method, so this is by no means a perfect solution. But there's no better way to achieve privacy.
现在只能从同一个文件中调用代码。您无法通过方法获得任何神奇的私有成员访问权限,因此这绝不是一个完美的解决方案。但是没有更好的方法来实现隐私。
回答by Raj
To implement hidden methods (instance and/or class)
实现隐藏方法(实例和/或类)
// ===========================
// = File: SomeClass.m
// ===========================
#import "SomeClass.h"
// =================================
// = Interface for hidden methods
// =================================
@interface SomeClass (hidden)
-(void) hiddenInstanceMethod;
@end
// ================================
// = Implementation for SomeClass
// ================================
@implementation SomeClass
-(void) hiddenInstanceMethod
{
printf( "Hidden instance method\n" );
}
-(void) msg
{
printf("Inside msg()...\n");
[self hiddenInstanceMethod];//private method calling
}
@end
http://macdevelopertips.com/objective-c/private-methods.html
http://macdevelopertips.com/objective-c/private-methods.html
reffer this link it will be helpful .
请参阅此链接,它将有所帮助。

