Objective-C:我应该声明私有方法吗? [英] Objective-C: Should I declare private methods?
问题描述
I've been declaring private methods in class extensions, according to Best way to define private methods for a class in Objective-C.
但是,我刚刚意识到,在Xcode 4中,如果我完全忽略了私有方法的声明并仅实现了它,则该应用程序将编译并运行,而不会发出警告或错误.
But, I just realized that, in Xcode 4, if I leave out the declaration of a private method altogether and just implement it, the app compiles and runs without warning or error.
那么,我是否还要在类扩展中声明私有方法?
So, should I even bother declaring private methods in class extensions?
为什么我们仍然必须声明方法?在Java中,您不会...在Ruby中也不会.
Why should we have to declare methods anyway? In Java, you don't... neither in Ruby.
推荐答案
仅当在方法之前声明了调用方时,才需要定义方法定义.为了保持一致,我建议在扩展中定义您的私有方法.
A method definition only needs to be defined if the caller is declared before the method. For consistency I would recommend defining your private methods in the extension.
-(void)somemethod
{
}
-(void)callermethod
{
//No warning because somemethod was implemented already
[self somemethod];
}
-(void)callermethod2
{
//Warning here if somemethod2 is not defined in the header or some extension
[self somemethod2];
}
-(void)somemethod2
{
}
这篇关于Objective-C:我应该声明私有方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!