如何修复此警告:“找不到方法(返回类型默认为“id”)”
我正在尝试从我的 iPhone 应用程序中删除其中一些警告。我得到最多的一个是当我有一个 void 函数并使用 [self myFunction]; 调用它时。警告位于我调用该函数的行上,它显示:“找不到方法‘-myFunction’(返回类型默认为‘id’)。”我需要做什么来解决这个问题?谢谢
I am trying to get rid of some of these warnings from my iPhone application. The one I get the most is when I have a void function and I call it using [self myFunction];. The warning is on the line where I call the function and it says: "Method '-myFunction' not found (return type defaults to 'id')." What do I need to do to fix this? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在头文件中声明函数,如下所示:
或
等等。
这比仅仅让编译器沉默更重要:如果函数的返回类型不是指针(
id
或任何*
),特别是当它没有相同的大小时,你可能会变得非常奇怪并且很难发现错误。例如,如果一个函数返回一个
CGRect
结构,并且编译器假定id
(如警告所示),就会发生非常奇怪的事情。Declare the function in your header file, like so:
or
etc etc.
This is of more importance than just silencing the compiler: if the return type of a function is not a pointer (
id
oranything*
), especially when it doesn't have the same size, you can get really odd and hard to find bugs.E.g. if a function returns a
CGRect
struct, and the compiler assumesid
(as the warning says), really weird things will happen.您需要在头文件 (.h) 中定义该函数(如果它对其他类可见),或者在实现文件 (.m) 中定义该函数(如果它对您的类来说是私有的)。
例如,如果您使用方法:
对于“私有”函数,请将其添加到 .m 文件的顶部;在
@implementation MyCoolClass
行之前。或者,如果您想从其他文件调用该方法,请将其添加到 .h 文件中的“interface”部分内;在所有属性之后、@end 之前。
祝你好运!
You need to define that function in your header file (.h) (if it should be visible to other classes), or in your implementation file (.m) if it is private to your class.
For example, if you use a method:
For a "private" function, add this at the top of your .m file; before the
@implementation MyCoolClass
line.OR if you want to call the method from other files, add it in your .h file, inside the 'interface' section; after all your properties, and before the @end.
Good luck!
您需要导入包含您的方法的 .h 文件
You need to import the .h file that contains yours methods
我遇到了类似的问题,这是因为我认为我已删除的项目中的不同子目录中有另一个同名的头文件。这最终使编译器认为该方法未在标头中声明,因为它在错误的标头文件中查找。
I had a similar issue pop up for me and it was because I had another header file with the same name in a different sub-directory within the project that I thought I had deleted. This was ultimately making the compiler think the method was not declared in the header because it was looking in the wrong header file.
我接下来决定这个问题:
--file: MyObjectController.h--
--file: MyObjectController.mm--
---file MyObject.h ----
---file MyObject.mm ----
I decide this problem next:
--file: MyObjectController.h--
--file: MyObjectController.mm--
---file MyObject.h ----
---file MyObject.mm ----
确保你有 -(void) myFunction;在头文件中声明,并确保 myFunction 不返回任何内容。
Ensure you have -(void) myFunction; declared in your header file and ALSO ensure that myFunction doesn't return anything.
只需确保该函数位于 .m 文件中的调用者之前即可!
我不知道这是否是正确的做事方式,但它确实消除了我的所有警告。
在苹果的示例代码中,我没有看到他们在 h 或 m 文件中声明所有方法。可以直接使用它。
Just make sure the function is placed before the caller in your .m file!!
I don't whether this is the correct way to do things but it does removed all those warning for me.
And in apple's sample code, I don't see they declare all methods in the h or m file. Can just use it straight away.
确保您正在使用的对象在其头文件中也声明了此函数。
sky.h
:sky.m
:Make sure that the object you're using has this function declared also in its header file.
sky.h
:sky.m
: