是否可以在Objective-C中将方法声明为私有?

时间:2022-05-17 10:29:18

Is it possible to declare a method as private in Objective-C?

是否可以在Objective-C中将方法声明为私有?

6 个解决方案

#1


34  

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.h file, you can add to your MyClass.m the 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 @implementation block 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社区采用“我告诉你不要把这种方法称为[通过将其置于私有类扩展或类别中,或者只是通过记录该方法是私有的]并且你仍然调用它。无论发生什么混乱都是你的错不要傻。“对这个问题的态度。

#2


2  

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."

不,任何对象都可以向任何其他对象发送任何消息。但是,您可以将该方法放在类的实现文件中。这样,如果您尝试在其他任何地方调用它,您将获得“类可能不会实现此方法”警告。这是使方法“私密”的正常方法。

#3


2  

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.

注意:不要声明这样的变量,否则它们将成为类变量 - 例如所有实例只使用一个变量。

#4


0  

You can do so by using categories. I've got a fuller description in my answer to this SO question.

您可以使用类别来完成此操作。我在回答这个问题时得到了更全面的描述。

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.

此外,您可以使用多个类来扩展类。因此,通过使用信息类别名称,您可以将私有函数分组到相关块中,从而改善代码的自我记录性质。

#5


0  

As others mentioned, you can't have code that's

正如其他人提到的,你不能拥有代码

  1. a method, and
  2. 一种方法,和
  3. impossible to call from outside a class.
  4. 不可能从课外打电话。

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.

现在代码只能在同一个文件中调用。您没有获得任何可以使用方法获得的魔法私有成员访问权限,因此这绝不是一个完美的解决方案。但是没有更好的方法来实现隐私。

#6


0  

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 .

提供此链接将有所帮助。

#1


34  

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.h file, you can add to your MyClass.m the 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 @implementation block 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社区采用“我告诉你不要把这种方法称为[通过将其置于私有类扩展或类别中,或者只是通过记录该方法是私有的]并且你仍然调用它。无论发生什么混乱都是你的错不要傻。“对这个问题的态度。

#2


2  

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."

不,任何对象都可以向任何其他对象发送任何消息。但是,您可以将该方法放在类的实现文件中。这样,如果您尝试在其他任何地方调用它,您将获得“类可能不会实现此方法”警告。这是使方法“私密”的正常方法。

#3


2  

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.

注意:不要声明这样的变量,否则它们将成为类变量 - 例如所有实例只使用一个变量。

#4


0  

You can do so by using categories. I've got a fuller description in my answer to this SO question.

您可以使用类别来完成此操作。我在回答这个问题时得到了更全面的描述。

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.

此外,您可以使用多个类来扩展类。因此,通过使用信息类别名称,您可以将私有函数分组到相关块中,从而改善代码的自我记录性质。

#5


0  

As others mentioned, you can't have code that's

正如其他人提到的,你不能拥有代码

  1. a method, and
  2. 一种方法,和
  3. impossible to call from outside a class.
  4. 不可能从课外打电话。

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.

现在代码只能在同一个文件中调用。您没有获得任何可以使用方法获得的魔法私有成员访问权限,因此这绝不是一个完美的解决方案。但是没有更好的方法来实现隐私。

#6


0  

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 .

提供此链接将有所帮助。