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

发布于 2024-10-21 21:53:57 字数 155 浏览 1 评论 0原文

我正在尝试从我的 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 技术交流群。

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

发布评论

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

评论(8

十二 2024-10-28 21:53:57

在头文件中声明函数,如下所示:

 -(void)myFunction;

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

等等。

这比仅仅让编译器沉默更重要:如果函数的返回类型不是指针(id任何*),特别是当它没有相同的大小时,你可能会变得非常奇怪并且很难发现错误。

例如,如果一个函数返回一个 CGRect 结构,并且编译器假定 id (如警告所示),就会发生非常奇怪的事情。

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.

E.g. if a function returns a CGRect struct, and the compiler assumes id (as the warning says), really weird things will happen.

甜警司 2024-10-28 21:53:57

您需要在头文件 (.h) 中定义该函数(如果它对其他类可见),或者在实现文件 (.m) 中定义该函数(如果它对您的类来说是私有的)。

例如,如果您使用方法:

-(void)myFunction {
    // do something
}

对于“私有”函数,请将其添加到 .m 文件的顶部;在 @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

或者,如果您想从其他文件调用该方法,请将其添加到 .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:

-(void)myFunction {
    // do something
}

For a "private" function, add this at the top of your .m file; before the @implementation MyCoolClass line.

@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; after all your properties, and before the @end.

Good luck!

街道布景 2024-10-28 21:53:57

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

You need to import the .h file that contains yours methods

没企图 2024-10-28 21:53:57

我遇到了类似的问题,这是因为我认为我已删除的项目中的不同子目录中有另一个同名的头文件。这最终使编译器认为该方法未在标头中声明,因为它在错误的标头文件中查找。

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.

魄砕の薆 2024-10-28 21:53:57

我接下来决定这个问题:

--file: MyObjectController.h--

#import "MyObject.h"

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

@end

--file: MyObjectController.mm--

#import "MyObjectController.h"

@implementation MyObjectController

-(void) makeSomething {}

@end

---file MyObject.h ----

@class MyObjectController;

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

@end

---file 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

I decide this problem next:

--file: MyObjectController.h--

#import "MyObject.h"

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

@end

--file: MyObjectController.mm--

#import "MyObjectController.h"

@implementation MyObjectController

-(void) makeSomething {}

@end

---file MyObject.h ----

@class MyObjectController;

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

@end

---file 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
思慕 2024-10-28 21:53:57

确保你有 -(void) myFunction;在头文件中声明,并确保 myFunction 不返回任何内容。

Ensure you have -(void) myFunction; declared in your header file and ALSO ensure that myFunction doesn't return anything.

眼睛会笑 2024-10-28 21:53:57

只需确保该函数位于 .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.

半山落雨半山空 2024-10-28 21:53:57

确保您正在使用的对象在其头文件中也声明了此函数。

sky.h

@interface sky : object {

}

-(void) doSomething:(int)param;

@end

sky.m

#import "sky.h"
@implementation sky

-(void) doSomething:(int)param

{

    //dosomething usefull 
}

Make sure that the object you're using has this function declared also in its header file.

sky.h:

@interface sky : object {

}

-(void) doSomething:(int)param;

@end

sky.m:

#import "sky.h"
@implementation sky

-(void) doSomething:(int)param

{

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