Objective-C什么时候在@interface中声明什么方法

时间:2023-01-15 14:25:35

When and what methods should be declared in the @interface section of a class? As I understand, methods that describe what your class does should be declared in the @interface section, but other "helper" methods should not be declared. Is this a correct understanding from my side?

什么方法应该在类的@interface部分声明?据我所知,描述你的类所做的方法应该在@interface部分声明,但不应声明其他“帮助”方法。这是我身边的正确理解吗?

3 个解决方案

#1


6  

You usually should add your methods to the .h file when you want an external class to have access to it (public methods).

当您希望外部类可以访问它时,您通常应该将方法添加到.h文件中(公共方法)。

When they're private (only used internally by the class) just put them in your .m file.

当它们是私有的(仅在类内部使用时),只需将它们放在.m文件中即可。

Anyway, it's just a pattern. As Objective-C works with messages, even if you don't set a method in your .h file an external file can access it, but at least your auto-complete won't show it.

无论如何,这只是一种模式。由于Objective-C适用于消息,即使您没有在.h文件中设置方法,外部文件也可以访问它,但至少您的自动完成不会显示它。

#2


6  

One way is to declare the instance methods in .h file. And, declare the private methods inside the .m, using a Category.

一种方法是在.h文件中声明实例方法。并且,使用类别声明.m内的私有方法。

For example, in MyOwnClass.h file.

例如,在MyOwnClass.h文件中。

@interface MyOwnClass

- (void)aInstanceMethod;

@end

And, inside your MyOwnClass.m file, before the @implementation block,

并且,在MyOwnClass.m文件中,在@implementation块之前,

@interface MyOwnClass (MyPrivateMethods)

- (void)aPrivateMethod;

@end

#3


1  

You should declare all your methods in your .h The tip from EmptyStack is nice but it's just a tip. If you don't intend to ship your binary as an SDK, you don't really need it.

你应该在你的.h中声明你所有的方法。来自EmptyStack的提示很好,但它只是一个提示。如果您不打算将二进制文件作为SDK发布,则实际上并不需要它。

Objective-C doesn't have (yet) private methods.

Objective-C没有(还)私有方法。

#1


6  

You usually should add your methods to the .h file when you want an external class to have access to it (public methods).

当您希望外部类可以访问它时,您通常应该将方法添加到.h文件中(公共方法)。

When they're private (only used internally by the class) just put them in your .m file.

当它们是私有的(仅在类内部使用时),只需将它们放在.m文件中即可。

Anyway, it's just a pattern. As Objective-C works with messages, even if you don't set a method in your .h file an external file can access it, but at least your auto-complete won't show it.

无论如何,这只是一种模式。由于Objective-C适用于消息,即使您没有在.h文件中设置方法,外部文件也可以访问它,但至少您的自动完成不会显示它。

#2


6  

One way is to declare the instance methods in .h file. And, declare the private methods inside the .m, using a Category.

一种方法是在.h文件中声明实例方法。并且,使用类别声明.m内的私有方法。

For example, in MyOwnClass.h file.

例如,在MyOwnClass.h文件中。

@interface MyOwnClass

- (void)aInstanceMethod;

@end

And, inside your MyOwnClass.m file, before the @implementation block,

并且,在MyOwnClass.m文件中,在@implementation块之前,

@interface MyOwnClass (MyPrivateMethods)

- (void)aPrivateMethod;

@end

#3


1  

You should declare all your methods in your .h The tip from EmptyStack is nice but it's just a tip. If you don't intend to ship your binary as an SDK, you don't really need it.

你应该在你的.h中声明你所有的方法。来自EmptyStack的提示很好,但它只是一个提示。如果您不打算将二进制文件作为SDK发布,则实际上并不需要它。

Objective-C doesn't have (yet) private methods.

Objective-C没有(还)私有方法。