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

如何解决收到非常奇怪的“不兼容的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;
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?