变得非常奇怪“不兼容的 Objective-C 类型”错误。也许其他人可以看到我哪里出错了?
这一定是我错过了一个小错误的事情之一,或者其他什么,但我似乎无法弄清楚。
Viewcontroller.h
#import "RGBEditView.h"
@interface ColorPickerView : UIViewController {
RGBEditView *rgbEditView;
}
-(void)showRGBEditor;
.m
-(void)showRGBEditor {
rgbEditView = [[RGBEditView alloc] initWithFrame:CGRectMake(0, 0, 280, 46) H:h S:s B:b];
}
正是上面的这一行,即 initwithframe 行,给出了错误 '不兼容的 Objective-C 类型分配 '*',预期 '*'
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: ,所以解决这个问题的唯一方法是将它们更改为有点不同的东西,但这似乎是一个尴尬的解决方法。还有其他解决方案吗?
It must be one of those things where there's a tiny mistake i've missed, or something, but i can't seem to figure it out.
Viewcontroller.h
#import "RGBEditView.h"
@interface ColorPickerView : UIViewController {
RGBEditView *rgbEditView;
}
-(void)showRGBEditor;
.m
-(void)showRGBEditor {
rgbEditView = [[RGBEditView alloc] initWithFrame:CGRectMake(0, 0, 280, 46) H:h S:s B:b];
}
It's this line above, the initwithframe line, that gives the error '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;
}
Can anybody see my problem? I'm very confused about this.
EDIT:
The problem lies in that I have another class which also uses initWithFrame:H:S:B:, so the only way to fix this is to change on of them to something a bit different, but this seems like an awkward work around. Any other solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
init
方法和以initWith
开头的方法应返回类型id
。通常发生的情况是,您有 2 个具有相同方法名称(在本例中为初始化程序)的类,但它们的返回类型不同:
RGBEditView
HSBEditView
alloc
返回 id - 编译器会警告您,因为它看到一个表达式这类似于以下示例中使用的类型分配:您可以通过从初始化程序返回 id 来纠正此问题,如下所示:
您返回 id 以避免像这样的冲突,并且因为编译器不知道是什么类型通过
alloc
返回,因此每个子类声明都必须返回不同的类型 - 这只会导致更多问题。例外情况是使用合格的名称 - 通常在便捷构造函数中看到:
the methods
init
and methods that start withinitWith
should return typeid
.what typically happens is that you have 2 classes with the same method name (initializer in this case), but differ in their return types:
RGBEditView
HSBEditView
alloc
returns id - the compiler warns you because it sees an expression which resembles type assignment used in the following example:you correct this by returning
id
from your initializers, like this:you return id to avoid clashes like this, and because the compiler doesn't know what type is returned via
alloc
, so every subclass declaration would have to return a different type - which would only lead to more problems.the exception to this is to use well qualified names - and is typically seen in convenience constructors:
在 RGBEditView.m 中尝试
in RGBEditView.m try
initWithFrame 应该是
initWithFrame should be