Obj-C私有方法编译器警告

时间:2011-08-04 12:13:58

标签: xcode methods private private-methods

私有方法是一种有用的结构,可以将代码组织在类边界内。一个例子是在自定义UIView子类中组织长文件的Quartz 2d指令。我能够在“.m”文件中包含这些方法,而在“.h”中没有声明。来自UIView子类'.m'文件的工作示例如下:

-(void)DoSomethingPrivate {                  //Not declared in interface
    NSLog(@"Does this print a private function?");
}

- (id)initWithFrame:(CGRect)frame            //Declared in inherited interface
{
    self = [super initWithFrame:frame];
    if (self) {
        [self DoSomethingPrivate];           //Error: 'Instance method not found'
    }                                        //... but it works anyway.
    return self;
}

我的问题是编译器在调用私有函数的行上生成警告“未找到实例方法'-DoSomethingPrivate'(返回类型默认为'id')”。我知道对this question的回复我可以使用'无名'接口类别来“隐藏”私有方法声明。

但是,当我查看Apple示例代码SimpleStocks,文件“SimpleStockView.m”时,它包含一个私有函数,它既未在无名类别接口中声明,也不会生成编译器警告:

//Private Function
    - (void)drawRadialGradientInSize:(CGSize)size centeredAt:(CGPoint)center { 
        ... 
    }

//Is called by this function...
    - (UIImage *)patternImageOfSize:(CGSize)size {
        ...
              //The next line doesn't generate any warnings!
        [self drawRadialGradientInSize:size centeredAt:center];
        ...
    }

如果有人能够了解Apple的示例代码私有方法如何逃避编译器检查,我将不胜感激,因此我可以避免使用所有私有方法维护“无名”类别标头。

非常感谢。

0 个答案:

没有答案