简体   繁体   English

如何修复此警告:“找不到方法(返回类型默认为'id')”

[英]How do I fix this warning: “Method not found (return type defaults to 'id')”

I am trying to get rid of some of these warnings from my iPhone application. 我试图摆脱我的iPhone应用程序中的一些警告。 The one I get the most is when I have a void function and I call it using [self myFunction];. 我得到的最多的是当我有一个void函数时,我用[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')." 警告在我调用函数的行上,它说:“找不到方法'-myFunction'(返回类型默认为'id')。” What do I need to do to fix this? 我需要做些什么来解决这个问题? Thanks 谢谢

Declare the function in your header file, like so: 在头文件中声明函数,如下所示:

 -(void)myFunction;

or 要么

 -(NSString*)myFunction;
 -(id)myFunction;
 -(NSInteger)myFunction;
 -(BOOL)myFunction;

etc etc. 等等

This is of more importance than just silencing the compiler: if the return type of a function is not a pointer ( id or anything* ), especially when it doesn't have the same size, you can get really odd and hard to find bugs. 这比仅仅沉默编译器更重要:如果函数的返回类型不是指针( idanything* ),特别是当它没有相同的大小时,你可能会变得奇怪而且很难找到bug 。

Eg if a function returns a CGRect struct, and the compiler assumes id (as the warning says), really weird things will happen. 例如,如果一个函数返回一个CGRect结构,并且编译器假定为id (如警告所示),那么真的会发生奇怪的事情。

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. 您需要在头文件(.h)中定义该函数(如果它应该对其他类可见),或者在实现文件(.m)中定义该函数(如果它对您的类是私有的)。

For example, if you use a method: 例如,如果您使用方法:

-(void)myFunction {
    // do something
}

For a "private" function, add this at the top of your .m file; 对于“私有”功能,请将其添加到.m文件的顶部; before the @implementation MyCoolClass line. @implementation MyCoolClass之前。

@interface MyCoolClass()  // <--- no 'category name' hides methods just for this file
-(void)myFunction;     // <--- add the method here
-(void)myOtherFunction;
-(void)doSomeCoolThingWithThisString:(NSString *)firstName;
@end

OR if you want to call the method from other files, add it in your .h file, inside the 'interface' section; 或者,如果要从其他文件中调用该方法,请将其添加到.h文件中,在“interface”部分中; after all your properties, and before the @end. 在所有属性之后,在@end之前。

Good luck! 祝好运!

您需要导入包含您的方法的.h文件

I decide this problem next: 我接下来决定这个问题:

--file: MyObjectController.h-- --file:MyObjectController.h--

#import "MyObject.h"

@interface MyObjectController
{
    MyObject * obj;
}
-(void) makeSomething;

@end

--file: MyObjectController.mm-- --file:MyObjectController.mm--

#import "MyObjectController.h"

@implementation MyObjectController

-(void) makeSomething {}

@end

---file MyObject.h ---- ---文件MyObject.h ----

@class MyObjectController;

@interface MyObject
{
    MyObjectController * objController;
}
-(void) callMakeSomethingFromObjController;

@end

---file MyObject.mm ---- ---文件MyObject.mm ----

#import "MyObject.h"  
#import "MyObjectController.h" //Add this line!!! And compiller will found makeSomething    

@implementation MyObject

-(void) callMakeSomethingFromObjController
{
     [objController makeSomething];//warning was here:instance method '-makeSomething' not found (return type defaults to 'id')  
}

@end

Ensure you have -(void) myFunction; 确保你有 - (void)myFunction; declared in your header file and ALSO ensure that myFunction doesn't return anything. 在头文件中声明,并确保myFunction不返回任何内容。

Just make sure the function is placed before the caller in your .m file!! 只需确保该函数放在.m文件中的调用者之前! 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. 在apple的示例代码中,我没有看到他们在h或m文件中声明所有方法。 Can just use it straight away. 可以直接使用它。

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. 这最终使编译器认为该方法未在标头中声明,因为它查找错误的头文件。

Make sure that the object you're using has this function declared also in its header file. 确保您正在使用的对象也在其头文件中声明了此函数。

sky.h : sky.h

@interface sky : object {

}

-(void) doSomething:(int)param;

@end

sky.m : sky.m

#import "sky.h"
@implementation sky

-(void) doSomething:(int)param

{

    //dosomething usefull 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 xcode警告“未找到-method&#39;imagePath&#39;(返回类型默认为&#39;id&#39;”) - xcode warning “-method 'imagePath' not found (return type defaults to 'id' ” 警告-找不到实例方法&#39;-initWithframe:&#39;(返回类型默认为&#39;id&#39;) - warning - Instance method '-initWithframe:' not found (return type defaults to 'id') 找不到实例方法(返回类型默认为id) - Instance method not found (return type defaults to id) 找不到方法(返回类型默认为“ id”) - Method not found (return type defaults to 'id') 方法“-cleanTitle”。 未找到(返回类型默认为 'id') - Method '-cleanTitle.' not found (return type defaults to 'id') 未找到实例方法&#39;-jsonValue&#39;(返回类型默认为&#39;id&#39;) - Instance method '-jsonValue'not found (return type defaults to 'id') 方法“-parseRss:条目。” 未找到(返回类型默认为 'id') - Method '-parseRss:entries.' not found (return type defaults to 'id') “ MainViewController /方法-x找不到之前的预期说明符列表”返回类型默认为id - Expected specifier-list before "MainViewController / Method -x not found return type defaults to id Iphone,我该如何修复此警告:&#39; - responseToSelector:&#39;在协议中找不到 - Iphone, how do I fix this warning: '-respondsToSelector:' not found in protocol(s) -base64Encoding返回类型默认为id - -base64Encoding return type defaults to id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM