正确的Objective-C的助手"崇拜者"私有方法? [英] Proper Objective-C Helper "Wannabe" Private methods?

查看:123
本文介绍了正确的Objective-C的助手"崇拜者"私有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我讨厌打马死刑这个问题(我已经通过这个各种文章阅读),而只是想获得在这个问题上更多的意见之前,我创造了我的自己的约定从现在开始使用上,而在Objective-C编码。

While I hate to beat a horse to death on this subject (I've read through various articles about this), but would just like to get more opinions on this matter before I create my "own convention" to use from now on while coding in Objective-C.

这是我想弄清楚的约定是最终如何(使用最佳的编码进行生产级代码的做法)使用一个私有方法类。在C#中的背景的,当我写班级,平时是在多个公共方法(如错误检查,或WCF服务的连接设置)重复一个代码块。我通常创建此代码的一个街区,并把它放在一个私有方法仅用于这些公共的方法来访问。这样,如果我需要做出改变,我只需要做的是在一个地方,而不是10个不同的地方的一类,但从来没有给用户调用这个私有方法的能力。例如:

The convention that I want to figure out is ultimately how to (using best coding practices for production level code) use private methods in a class. Coming from a background in C#, when I write classes, usually there is a block of code that is repeated in multiple public methods (such as error checking, or WCF service connection setup). I usually create one block of this code and put it in a private method for only these public methods to access. This way if I need to make a change, I only need to do it in one spot, as opposed to 10 different places in a class, but then never giving users the ability to call this private method. For example:

public Class A
{
    public void method1()
    {
        doErrorChecking()
        // Do more stuff
    }
    public void method2()
    {
        doErrorChecking()
        // Do more stuff
    }     
    private doErrorChecking() { //Error Checking Code}
}

据我所知,没有真正的办法真正让这最后的方法在Objective-C专用,只是真的想确保当我创建的Objective-C的所有未来的类iOS开发我继在这个问题上的最佳实践可以使未来的代码重构将不再需要(希望)。我注意到人们谈论的类别,其他人只是不要把方法在@interface文件中,和其他人使用扩展方法。此刻,我只是把在@implementation文件的方法实现,而不是接口文件。我也使得崇拜者私有方法有一个明显的真名,这样子类或覆盖的方法是不是一个问题。这是我应该遵循的道路?或者,这些特定情况下有没有更好的办法做到这一点?

I understand that there is no real way to truly make that last method private in Objective-C, but just really want to make sure that when I create all future classes in Objective-C for iOS development I'm following the best practice available so future code refactoring on this matter won't be needed (hopefully). I've noticed people talking about categories, others just don't put the method in the @interface file, and others use extension methods. At the moment I'm just putting the method implementation in the @implementation file, but not the interface file. I'm also making the "wannabe" private method have a really distinct name so that sub-classing or overwriting methods is not an issue. Is this the path I should be following? Or for these particular scenarios is there a better way to do it?

推荐答案

是的,这是完全合理的,要提取的功能出到另一种方法。在我看来,要做到这一点,最好的方法是使用的类延续,你可以把你的私有方法声明中,它可以去上面的 @implementation 块在 .M 文件,所以它不是公共头。

Yes, it's perfectly reasonable to want to extract your functionality out into another method. The best way to do this in my opinion is using a class continuation, which you can put your private method declarations in. It can go above your @implementation block in your .m file, so it's not in the public header.

@interface MyClass ()
- (void)_privateMethod:(id)arg;
@end



(一类延续和正常类别之间的差异,如 @interface MyClass的(PrivateMethods))是编译器将要求您实现在主 @implementation 块的方法,而不是有一个单独的 @implementation MyClass的(PrivateMethods)块。实现辅助方法,当你这样描述这无疑是可取的。

The difference between a class continuation and a normal category (such as @interface MyClass (PrivateMethods)) is that the compiler will require you to implement the methods in your main @implementation block, rather than having a separate @implementation MyClass (PrivateMethods) block. This is arguably desirable when implementing helper methods like you described.

在命名方面,这是比较常见的启动私有方法的名称(与伊娃的名字,因为这物)与 _ ,虽然不是每个人都没有 - 显然苹果的reserves该为自己,所以你应该选择一个不同的前缀。语言不强制任何东西。

In terms of naming, it's relatively common to start private method names (and ivar names, for that matter) with an _, though not everyone does — apparently Apple reserves this for themselves, so you should pick a different prefix. The language doesn't enforce anything.

这篇关于正确的Objective-C的助手"崇拜者"私有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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