收到非常奇怪的“不兼容的Objective-C类型”错误也许其他人可以看到我要去哪里错了?

问题描述

| 这肯定是我错过的一个错误之类的东西之一,但是我似乎无法弄清楚。 Viewcontroller.h
#import \"RGbeditView.h\"
@interface ColorPickerView : UIViewController {
RGbeditView *rgbeditView;
}
-(void)showRGbeditor;
.m
-(void)showRGbeditor {
rgbeditView = [[RGbeditView alloc] initWithFrame:CGRectMake(0,280,46) H:h S:s B:b];
}
这是上面的这一行,initwithframe行,给出错误
\'Incompatible Objective-C types assigning \'*\',expected \'*\'
RGbeditView.h
@interface RGbeditView : UIView {
}
-(RGbeditView *)initWithFrame:(CGRect)frame H:(float)hue S:(float)saturation B:(float)brightness;
RGbeditView.m
-(RGbeditView *)initWithFrame:(CGRect)frame H:(float)hue S:(float)saturation B:(float)brightness {
[super initWithFrame:frame];
 return self;
}
有人可以看到我的问题吗?我对此很困惑。 编辑: 问题在于,我还有另一个也使用initWithFrame:H:S:B:的类,因此解决此问题的唯一方法是将它们更改为一些不同的东西,但这似乎很尴尬。还有其他解决方案吗?     

解决方法

        方法
init
和以
initWith
开头的方法应返回类型
id
。 通常发生的是,您有两个具有相同方法名的类(在这种情况下为初始化器),但是它们的返回类型不同: RGBEditView
  -(RGBEditView *)initWithFrame:(CGRect)frame H:(float)h S:(float)s B:(float)b;
HSBEditView
  -(HSBEditView *)initWithFrame:(CGRect)frame H:(float)h S:(float)s B:(float)b;
alloc
返回id-编译器警告您,因为它看到类似于以下示例中使用的类型分配的表达式:
RGBEditView * rgb = /* ... */;
HSBEditView * hsb = nil;
hsb = rgb // << compiler: \"hey - you don\'t want to do that unless
          //               RGBEditView were a subclass of
          //               HSBEditView... but it\'s not!\"
您可以通过从初始值设定项中返回
id
来更正此错误,如下所示:
-(id)initWithFrame:(CGRect)frame H:(float)h S:(float)s B:(float)b;
您应返回id以避免此类冲突,并且由于编译器不知道通过
alloc
返回的类型,因此每个子类声明都必须返回不同的类型-这只会导致更多问题。 例外是使用合格的名称-通常在便捷构造函数中可见:
+ (HSBEditView *)newHSBEditViewWithFrame:(CGRect)frame
                                       H:(float)h S:(float)s B:(float)b;
    ,        在RGBEditView.m中尝试
self = [super initWithFrame:frame];
return self;
    ,        initWithFrame应该是
-(RGBEditView *)initWithFrame:(CGRect)frame H:(float)hue S:(float)saturation B:(float)brightness {
    self = [super initWithFrame:frame];
    return self;
}
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...