在Objective-C中为类定义私有方法的最佳方法 [英] Best way to define private methods for a class in Objective-C

查看:191
本文介绍了在Objective-C中为类定义私有方法的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始编写Objective-C,并且在Java中有一个背景,想知道编写Objective-C程序的人如何处理私有方法。



有几个约定和习惯,并认为这个问题是人们在Objective-C中使用处理私有方法的最佳技术的聚合器。



发布时请附上您的方法的参数。为什么是好?






至于我的发现。



可以使用类别 [例如



这个方法有两个问题:


  1. Xcode(和compiler?)不会检查是否在相应的@implementation块中的私有类别中定义了所有方法

  2. 您必须将@interface声明为private类别在MyClass.m文件的开头,否则Xcode会提示self可能不响应消息privateFoo。

第一个问题可以通过空类别 [eg MyClass()]。

第二个问题困扰我很多,我想看看私有方法的实现

解决方案

没有,因为其他人已经说过,这样的事情是Objective-C中的一个私有方法。但是,从Objective-C 2.0开始(意味着Mac OS X Leopard,iPhone OS 2.0及更高版本),您可以创建一个具有空名称的类别(即 @interface MyClass())调用类扩展。类扩展的独特之处在于方法实现必须与公共方法一样位于相同的 @implementation MyClass 中。所以我这样构造我的类:



在.h文件中:

  @interface MyClass {
//我的实例变量
}

- (void)myPublicMethod;

@end

在.m文件中:

  @interface MyClass()

- (void)myPrivateMethod;

@end

@implementation MyClass

- (void)myPublicMethod {
//执行到这里
}

- (void)myPrivateMethod {
//实现在这里
}

@end
pre>

我认为这种方法的最大优点是它允许你通过功能来分组方法实现,而不是通过(有时是任意的)公共/私人区分。 / p>

I just started programming Objective-C and, having a background in Java, wonder how people writing Objective-C programs deal with private methods.

I understand there may be several conventions and habits and think about this question as an aggregator of the best techniques people use dealing with private methods in Objective-C.

Please include an argument for your approach when posting it. Why is it good? Which drawbacks does it have (that you know of) and how you deal with them?


As for my findings so far.

It is possible to use categories [e.g. MyClass (Private)] defined in MyClass.m file to group private methods.

This approach has 2 issues:

  1. Xcode (and compiler?) does not check if you define all methods in private category in corresponding @implementation block
  2. You have to put @interface declaring your private category in the begin of MyClass.m file, otherwise Xcode complains with a message like "self may not respond to message "privateFoo".

The first issue can be worked around with empty category [e.g. MyClass ()].
The second one bothers me a lot. I'd like to see private methods implemented (and defined) near the end of the file; I do not know if that's possible.

解决方案

There isn't, as others have already said, such a thing as a private method in Objective-C. However, starting in Objective-C 2.0 (meaning Mac OS X Leopard, iPhone OS 2.0, and later) you can create a category with an empty name (i.e. @interface MyClass ()) called Class Extension. What's unique about a class extension is that the method implementations must go in the same @implementation MyClass as the public methods. So I structure my classes like this:

In the .h file:

@interface MyClass {
    // My Instance Variables
}

- (void)myPublicMethod;

@end

And in the .m file:

@interface MyClass()

- (void)myPrivateMethod;

@end

@implementation MyClass

- (void)myPublicMethod {
    // Implementation goes here
}

- (void)myPrivateMethod {
    // Implementation goes here
}

@end

I think the greatest advantage of this approach is that it allows you to group your method implementations by functionality, not by the (sometimes arbitrary) public/private distinction.

这篇关于在Objective-C中为类定义私有方法的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆