iOS:太多 initWithDelegate 消息和编译器警告

发布于 2024-11-15 16:01:32 字数 304 浏览 3 评论 0原文

我的一个视图控制器依赖于几个类,每个类都有一个 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 技术交流群。

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

发布评论

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

评论(3

恬淡成诗 2024-11-22 16:01:32

听起来好像类型不匹配或编译器不知道。不要忘记将 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 an id and that delegate is of type id 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.

为人所爱 2024-11-22 16:01:32

我发现抑制警告的一个简单方法是执行以下操作:

[(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.

舞袖。长 2024-11-22 16:01:32

请注意,alloc 返回 id,因此编译器不知道 [CLASS_1 alloc] 返回 CLASS_1 类型的对象。它通常会推断出正确的类型并且不会添加警告,但有时也不会。我通常的解决方案是强制转换。丑陋,但它有效。

[(CLASS_1*)[CLASS_1 alloc] initWithDelegate:self];

Note that alloc returns id, 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.

[(CLASS_1*)[CLASS_1 alloc] initWithDelegate:self];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文