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

发布于 2024-11-10 02:59:26 字数 972 浏览 0 评论 0原文

这一定是我错过了一个小错误的事情之一,或者其他什么,但我似乎无法弄清楚。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑饮青盏花 2024-11-17 02:59:26

init 方法和以 initWith 开头的方法应返回类型 id

通常发生的情况是,您有 2 个具有相同方法名称(在本例中为初始化程序)的类,但它们的返回类型不同:

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;

the methods init and methods that start with initWith should return type id.

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

  -(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 returns id - the compiler warns you because it sees an expression which resembles type assignment used in the following example:

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!"

you correct this by returning id from your initializers, like this:

-(id)initWithFrame:(CGRect)frame H:(float)h S:(float)s B:(float)b;

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:

+ (HSBEditView *)newHSBEditViewWithFrame:(CGRect)frame
                                       H:(float)h S:(float)s B:(float)b;
独自←快乐 2024-11-17 02:59:26

在 RGBEditView.m 中尝试

self = [super initWithFrame:frame];
return self;

in RGBEditView.m try

self = [super initWithFrame:frame];
return self;
弥繁 2024-11-17 02:59:26

initWithFrame 应该是

-(RGBEditView *)initWithFrame:(CGRect)frame H:(float)hue S:(float)saturation B:(float)brightness {
    self = [super initWithFrame:frame];
    return self;
}

initWithFrame should be

-(RGBEditView *)initWithFrame:(CGRect)frame H:(float)hue S:(float)saturation B:(float)brightness {
    self = [super initWithFrame:frame];
    return self;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文