私有方法显示为公共方法

发布于 2024-09-25 05:09:52 字数 410 浏览 0 评论 0原文

我正在尝试使用私有方法来改进我的应用程序的设计。来自 .NET 我有点困惑,因为我在 .m 文件中声明了这些方法,但从其他文件中它们仍然显示,即它们仍然可以访问。

.m 文件:

@interface NSContentWebServiceController (private)

- (NSString *)flattenHTML:(NSString *)html;
- (NSString *)cleanseStringOfJsonP:(NSString *)jsonP;
- (void)retrieve:(NSasdf *)hasdel :(NSDictionary *)rootList;   
- (NSString *)removeHTMLTagsFromString:(NSString *)aString;

@end

I am trying to improve the design of my App by using private methods. Coming from .NET I am a little confused because I am declaring these methods in the .m file but from other files they are still showing up i.e. they are still accessible.

.m file:

@interface NSContentWebServiceController (private)

- (NSString *)flattenHTML:(NSString *)html;
- (NSString *)cleanseStringOfJsonP:(NSString *)jsonP;
- (void)retrieve:(NSasdf *)hasdel :(NSDictionary *)rootList;   
- (NSString *)removeHTMLTagsFromString:(NSString *)aString;

@end

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

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

发布评论

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

评论(2

胡大本事 2024-10-02 05:09:52

正如 JoostK 所说,Objective-C 中没有像 C++、Java 或 C# 中那样的私有方法。

最重要的是,表达式 @interface NSContentWebServiceController (private) 定义了 Objective-C 中所谓的类别。这里的术语private仅仅是类别的名称,没有任何意义。这里有类似 yellowBunny 的东西会产生相同的效果。类别只是将一个类分解为几个部分的一种方法,但在运行时所有类别都有效。请注意,类别只能向对象类添加新方法,但不能添加新变量。

对于私有类别,现在首选使用匿名类别,如 @interface MyClass() 中,因为这样您就不需要单独的 @implementation MyClass(yellowBunny) 块但只需将方法添加到主 @implementation 块中即可。

有关详细信息,请参阅 Objective-C 维基百科条目中的“类别”部分

As JoostK said, there are no private methods in Objective-C like you have them in C++, Java or C#.

On top of that, the expression @interface NSContentWebServiceController (private) defines a so-called category in Objective-C. The term private here is merely a name for the category and has no meaning. Having something like yellowBunny in here would yield the same effect. A category is merely a way to break down a class into several pieces, but at runtime all categories are in effect. Note that a category is only able to add new methods to an object class, but not new variables.

For private categories it's now preferred to use the anonymous category, as in @interface MyClass(), as you then don't need a separate @implementation MyClass(yellowBunny) block but can just add the methods to main @implementation block.

See the "Categories" section in the Wikipedia entry on Objective-C for more information.

白日梦 2024-10-02 05:09:52

私有方法仅以未记录在头文件中的方式是私有的。因此,您无法将它们#import到您的项目中,因此编译器会警告您“选择器无法识别”或类似的情况。

您将能够像公共方法一样调用这些方法,因为它只是您声明使方法私有的原型,Objective-C 没有隐藏的、真正私有的东西, 方法。

在运行时,您将始终能够使用内省找到所有方法,因此实际上没有办法完全隐藏您的方法/属性。

您可以添加一个 id _internal 实例变量,该变量指向执行所有工作的对象,这样调用私有方法会有点困难,尽管并非不可能。

Private methods are only private in a way that they're not documented in a header file. Because of this you can't #import them into your project and thus will the compiler warn you about a 'selector not recognized' or something like that.

You'll be able to call these methods just as public methods, since it's just where you declare the prototype that makes a method private, Objective-C doesn't have such a thing as hidden, really private, methods.

At runtime, you will always be able to find all methods using introspection, so there really is no way of completely hiding your methods/properties.

You could add a id _internal instance variable which points to an object that does all the work, that way it's a bit more tough to call the private methods, although not impossible.

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