Objective-C:我应该声明私有(private)方法吗?

标签 objective-c categories private-methods class-extensions

根据 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/

相关文章:

javascript - 如何使用私有(private)方法正确配置 ES6 类的 eslint?

objective-c - %qx 格式说明符是什么意思

ios - MPMoviePlayerViewController 移除 "Audio and Subtitles"控制

magento - 在 magento 中显示所有子类别及其子类别

JavaScript 私有(private)方法

google-play - 如何分发仅供员工内部使用的应用程序

iphone - Core Data 数据模型 : attribute type for UIColor

ios - Parse.com WhereKey :containedIn doesn`t show any results - objectId

java - Wicket 口的类别面板

iphone - 向我自己的类(class)添加类别