如何在 Objective-c 中将类中的单个方法设为私有?

发布于 2024-08-23 18:57:45 字数 37 浏览 4 评论 0原文

如何在 Objective-c 中将类中的单个方法设为私有?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听风吹 2024-08-30 18:57:45

Objective-C 中没有私有方法的概念。然而,您可以做的是忽略头文件中的方法并在私有文件中添加类别。

通常是这样的:

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

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

类别的空名称意味着它是 类扩展,这告诉编译器该方法在主实现块中是必需的。

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.

心房的律动 2024-08-30 18:57:45

严格来说,你不能在 Objective-C 中创建私有方法;但是您可以在包含实现的文件中创建一个类别 (MyClass.m),将其隐藏在头文件 (MyClass.h) 中。

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

@interface MyClass : NSObject {

}

@end

实施文件:

//MyClass.m

@interface MyClass ()

- (void)myPrivateMethod;

@end

@implementation MyClass

- (void)myPrivateMethod {
  return;
}

@end

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
羁〃客ぐ 2024-08-30 18:57:45

Objective-C 在设计上本身并不支持私有方法。

但是,您可以通过使用“类扩展”(匿名类别)来实现私有方法控制。

基本思想是在类的实现文件(而不是其标头)内,但在 @implementation 块之外,使用私有方法声明类扩展。然后您可以实现这些方法,并且外部类将无法看到它们。像这样:

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

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

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

使用类扩展强制您实现主 @implementation 块中的方法,并要求实现它们(就像它们在标头的主 @interface 块中声明一样)。

不幸的是,由于 Objective-C 的动态特性,这实际上不会阻止其他类调用这些方法。他们将收到一条警告,告诉他们该类可能不会响应该消息,但在运行时它将起作用。这给您带来的唯一好处是能够向其他程序员隐藏这些方法。

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.

不气馁 2024-08-30 18:57:45

你不能。只有实例变量可以标记为私有。

尽管有一些方法可以做相当于私有方法的事情。
一般的想法是,您不在标头中声明私有方法,而是在实现文件中声明私有方法。

因此,如果您想要一个公共 (myPublicMethod) 和私有 (myPrivateMethod) 方法,您的标头可能如下所示:

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

然后您的实现文件有三个选项:

1.

不要在任何 @interface 部分中声明该方法,只需确保它在 @implementation 部分中使用之前已实现即可。

@implementation MyClass

- (void)myPrivateMethod {  }

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

@end

2.

在实现文件中使用匿名类别来声明私有方法并在主 @implementation 块中实现它们。

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

@implementation MyClass

- (void)myPrivateMethod {  }

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

@end

3.

在实现文件中使用常规类别,并在单独的 @implementation 块中实现私有方法。

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

@implementation MyClass (PrivateMethods)

- (void)myPrivateMethod {  }

@end

@implementation MyClass

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

@end

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文