iOS:太多 initWithDelegate 消息和编译器警告
我的一个视图控制器依赖于几个类,每个类都有一个 initWithDelegate: 方法。在所有情况下,视图控制器都是委托。 我在所有调用上都会收到编译器警告
[[ONE_OF_FOUR_CLASSES] alloc] initWithDelegate:self];
除了第一个调用(首先加载谁的头文件)之外,
。所有其他的都会给出“从 yy 分配给 xx 的不兼容的指针类型”警告。 yy 始终是 4 类中的第一个。
不过,一切都运行良好。那么我该如何抑制这些警告呢?这是 LLVM 错误吗?
One of my view controllers relies on several classes that each have an initWithDelegate: method. The view controller is the delegate in all cases. I get compiler warnings on all calls to
[[ONE_OF_FOUR_CLASSES] alloc] initWithDelegate:self];
except for the first (who's header file is loaded first).
All the others give an "incompatible pointer types assigning to xx from yy" warning. yy is always that first of the 4 classes.
Everything runs fine, however. So how do I suppress these warnings? Is this an LLVM bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来好像类型不匹配或编译器不知道。不要忘记将
init
方法的签名添加到 .h 文件中并导入它们。检查它们是否返回id
并且delegate
也是id
类型(至少这是您大多数时候想要的)。如果这不能解决问题,请随意编辑您的问题以包含更多代码。
Sounds as if the types doesn't match or are not known to the compiler. Don't forget to add the signatures of your
init
-methods to your .h files and import them. Check that they return anid
and thatdelegate
is of typeid
as well (at least that's what you want most of the time).Feel free to edit your question to include more code if this doesn't clear things up.
我发现抑制警告的一个简单方法是执行以下操作:
[(ONE_OF_FOUR_CLASSES *)[ONE_OF_FOUR_CLASSES alloc] initWithDelegate:self];
此强制转换使编译器能够实现 +( 的返回值id)alloc 调用不是“id”,但实际上是您的 ONE_OF_FOUR_CLASSES。
I found an easy way to suppress the warning was to do the following:
[(ONE_OF_FOUR_CLASSES *)[ONE_OF_FOUR_CLASSES alloc] initWithDelegate:self];
This cast enables the compiler to realise the return value of the +(id)alloc call is not 'id' but is actually your ONE_OF_FOUR_CLASSES.
请注意,
alloc
返回id
,因此编译器不知道 [CLASS_1 alloc] 返回 CLASS_1 类型的对象。它通常会推断出正确的类型并且不会添加警告,但有时也不会。我通常的解决方案是强制转换。丑陋,但它有效。Note that
alloc
returnsid
, so the compiler doesn't know that [CLASS_1 alloc] returns an object of type CLASS_1. It usually infers the type right and doesn't add the warning, but sometimes it doesn't. My usual solution is a cast. Ugly, but it works.