根据 Best way to define private methods for a class in Objective-C,我一直在类扩展中声明私有(private)方法.
但是,我刚刚意识到,在 Xcode 4 中,如果我完全省略私有(private)方法的声明而只是实现它,应用程序会在没有警告或错误的情况下编译和运行。
那么,我是否应该费心在类扩展中声明私有(private)方法?
为什么我们必须声明方法呢?在 Java 中,您不需要...在 Ruby 中也不会。
最佳答案
如果调用者在方法之前声明,则只需要定义方法定义。为了保持一致性,我建议在扩展中定义您的私有(private)方法。
-(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:我应该声明私有(private)方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6767129/