私有接口与私有方法 - 目标c

时间:2021-04-01 22:50:46

What is the difference between a private method and a private interface? For example, I know that if you define a method in an implementation and its interface does not mention it, it is considered a private method. I have also seen things such as:

私有方法和私有接口有什么区别?例如,我知道如果你在一个实现中定义一个方法而它的接口没有提到它,它就被认为是一个私有方法。我也见过如下事情:

@interface Collector()
@property (readonly) NSMutableDictionary *count;
@end

Inside of the .m implementation file.

在.m实现文件中。

2 个解决方案

#1


20  

@interface Foo() creates a class extension (I stand corrected, props to bbum) on interface Foo which is like additional methods added to the interface. Some people also use @interafce Foo(Private) (category) instead of a class extension with (). It's more like "injecting" new methods into a class from outside the class.

@interface Foo()在接口Foo上创建了一个类扩展(我更正,道具到bbum),就像添加到接口的其他方法一样。有些人也使用@interafce Foo(私有)(类别)而不是带()的类扩展。它更像是从类外部“注入”新方法到类中。

Placing this in the .m file just keeps other things from "seeing it" in the .h file, but that's it. Basically people normally use categories or class extensions in .m files to specify private interfaces, but they are also used for things like UIKit uses categories to add row and section public methods to NSIndexPath. (This can be confusing.)

将它放在.m文件中只是让其他东西不会在.h文件中“看到它”,但就是这样。基本上人们通常在.m文件中使用类别或类扩展来指定私有接口,但它们也用于诸如UIKit使用类别向NSIndexPath添加行和节公共方法之类的东西。 (这可能令人困惑。)

You don't really need to define private methods this way, but if you have a method called bar that calls method foo before foo is defined in the source file you'll get a compiler warning something like "object self may not respond to foo". You can get rid of that by defining foo before you define bar or any other foo-calling code. It's the same with plain C and functions.

你真的不需要以这种方式定义私有方法,但如果你有一个名为bar的方法,在源文件中定义foo之前调用方法foo,你会得到一个编译器警告,例如“对象自身可能无法响应foo” ”。您可以在定义bar或任何其他foo调用代码之前通过定义foo来摆脱它。它与普通的C和函数相同。

Like Ole says this doesn't stop anyone from calling the private methods, it just declares your intention that they be private and causes the compiler to generate the "may not respond to" warnings even if they import the .h file.

就像Ole说这不会阻止任何人调用私有方法,它只是声明你的意图是私有的,并导致编译器生成“可能没有响应”警告,即使它们导入.h文件。

EDIT

编辑

Also see http://www.friday.com/bbum/2009/09/11/class-extensions-explained/ for some explanation of categories vs. class extensions. Looks like class extensions should be more correct for defining private methods, from a compiler warning perspective, because category methods are optional. Wish my book would have explained this!

另请参阅http://www.friday.com/bbum/2009/09/11/class-extensions-explained/,了解类别与类扩展的一些解释。从编译器警告的角度看,类扩展应该更准确地定义私有方法,因为类别方法是可选的。希望我的书能够解释这个!

#2


17  

Objective-C has no totally private methods. The method declared in a private interface section in the .m file is invisible to outside callers but it is not private. If someone knows the method signature and ignores the compiler warning, they can call it from outside without problems.

Objective-C没有完全私有的方法。在.m文件的专用接口部分中声明的方法对外部调用者是不可见的,但它不是私有的。如果有人知道方法签名并忽略编译器警告,他们可以从外部调用它而不会出现问题。

#1


20  

@interface Foo() creates a class extension (I stand corrected, props to bbum) on interface Foo which is like additional methods added to the interface. Some people also use @interafce Foo(Private) (category) instead of a class extension with (). It's more like "injecting" new methods into a class from outside the class.

@interface Foo()在接口Foo上创建了一个类扩展(我更正,道具到bbum),就像添加到接口的其他方法一样。有些人也使用@interafce Foo(私有)(类别)而不是带()的类扩展。它更像是从类外部“注入”新方法到类中。

Placing this in the .m file just keeps other things from "seeing it" in the .h file, but that's it. Basically people normally use categories or class extensions in .m files to specify private interfaces, but they are also used for things like UIKit uses categories to add row and section public methods to NSIndexPath. (This can be confusing.)

将它放在.m文件中只是让其他东西不会在.h文件中“看到它”,但就是这样。基本上人们通常在.m文件中使用类别或类扩展来指定私有接口,但它们也用于诸如UIKit使用类别向NSIndexPath添加行和节公共方法之类的东西。 (这可能令人困惑。)

You don't really need to define private methods this way, but if you have a method called bar that calls method foo before foo is defined in the source file you'll get a compiler warning something like "object self may not respond to foo". You can get rid of that by defining foo before you define bar or any other foo-calling code. It's the same with plain C and functions.

你真的不需要以这种方式定义私有方法,但如果你有一个名为bar的方法,在源文件中定义foo之前调用方法foo,你会得到一个编译器警告,例如“对象自身可能无法响应foo” ”。您可以在定义bar或任何其他foo调用代码之前通过定义foo来摆脱它。它与普通的C和函数相同。

Like Ole says this doesn't stop anyone from calling the private methods, it just declares your intention that they be private and causes the compiler to generate the "may not respond to" warnings even if they import the .h file.

就像Ole说这不会阻止任何人调用私有方法,它只是声明你的意图是私有的,并导致编译器生成“可能没有响应”警告,即使它们导入.h文件。

EDIT

编辑

Also see http://www.friday.com/bbum/2009/09/11/class-extensions-explained/ for some explanation of categories vs. class extensions. Looks like class extensions should be more correct for defining private methods, from a compiler warning perspective, because category methods are optional. Wish my book would have explained this!

另请参阅http://www.friday.com/bbum/2009/09/11/class-extensions-explained/,了解类别与类扩展的一些解释。从编译器警告的角度看,类扩展应该更准确地定义私有方法,因为类别方法是可选的。希望我的书能够解释这个!

#2


17  

Objective-C has no totally private methods. The method declared in a private interface section in the .m file is invisible to outside callers but it is not private. If someone knows the method signature and ignores the compiler warning, they can call it from outside without problems.

Objective-C没有完全私有的方法。在.m文件的专用接口部分中声明的方法对外部调用者是不可见的,但它不是私有的。如果有人知道方法签名并忽略编译器警告,他们可以从外部调用它而不会出现问题。