Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 警告";实例方法&x27-方法名称';未找到(返回类型默认为';id';)";虽然它存在并且运行良好_Objective C_Cocoa - Fatal编程技术网

Objective c 警告";实例方法&x27-方法名称';未找到(返回类型默认为';id';)";虽然它存在并且运行良好

Objective c 警告";实例方法&x27-方法名称';未找到(返回类型默认为';id';)";虽然它存在并且运行良好,objective-c,cocoa,Objective C,Cocoa,我发现一些类似的问题属于这一警告,但它们没有得到回答或没有真正准确地提出 我的AppDelegate有一个名为AppModel*iVarModel的实例变量。 App委托有一个声明的属性,同时还有一个单独的getter,如: AppDelegate.h AppModel* iVarModel; @property (nonatomic, retain) AppModel* iVarModel; - (AppModel*) getAppModel; AppDelegate.m @

我发现一些类似的问题属于这一警告,但它们没有得到回答或没有真正准确地提出

我的AppDelegate有一个名为AppModel*iVarModel的实例变量。 App委托有一个声明的属性,同时还有一个单独的getter,如:

AppDelegate.h

AppModel* iVarModel;    

@property (nonatomic, retain) AppModel* iVarModel;  

- (AppModel*) getAppModel;
AppDelegate.m

@synthesize iVarModel;  

- (AppModel*) getAppModel {
  return iVarModel;
}
//get a pointer to the application object
UIApplication* thisApp = [UIApplication sharedApplication];

// get a pointer to the application's delegate
id<UIApplicationDelegate> theDelegateObject = [thisApp delegate];


// >(1)< access AppModel's property iVarModel
AppModel* iVarModel_byProp = [theDelegateObject iVarModel];

// >(2)< access AppModel's iVarModel via getter
AppModel* iVarModel_byGet = [theDelegateObject getAppModel];
在另一个班级 我喜欢通过singleton应用程序对象来访问它:

FarrawayClass.h

import "AppDelegate.h"
...
FarrawayClass.m

@synthesize iVarModel;  

- (AppModel*) getAppModel {
  return iVarModel;
}
//get a pointer to the application object
UIApplication* thisApp = [UIApplication sharedApplication];

// get a pointer to the application's delegate
id<UIApplicationDelegate> theDelegateObject = [thisApp delegate];


// >(1)< access AppModel's property iVarModel
AppModel* iVarModel_byProp = [theDelegateObject iVarModel];

// >(2)< access AppModel's iVarModel via getter
AppModel* iVarModel_byGet = [theDelegateObject getAppModel];
为什么编译器认为这些方法不存在,即使他可以正确地使用它们


顺便说一句,如果我跳过getter或声明的属性,则不会有任何区别。我总是收到此警告。

我最近遇到了类似的问题,并发现(天知道如何)我的项目在不同的位置有重复的AppDelegate.h和AppDelegate.m文件。我正在编辑一个,而编译器正在使用另一个,这意味着它无法看到我添加的实例方法!删除重复文件解决了问题。

编译器警告您,因为您告诉编译器此局部变量“theDelegateObject”的类型为“id”。换句话说,您说是“某个NSObject实现了UIApplicationLegate协议”。您没有说它是特定AppDelegate类的实例

如果您这样做,编译器将知道您在做什么:

MyAppDelegate* theDelegateObject = (MyAppDelegate*) [thisApp delegate];
这为编译器提供了它需要知道的类型信息,该对象应该响应您自己编写的方法

至于为什么它在运行时可以正常工作,请记住消息传递是动态的,编译器不需要在编译时绑定到此方法。它只是尽职尽责地编写了您请求的方法调用。在运行时,这是因为应用程序的委托是AppDelegate类的实际实例


希望这有意义?

我自己发现的。要让您知道并让其他人找到解决方案,请执行以下操作:

正如我试图做到的那样:

FarFarAwayClass.m(第1版)


所有警告均已消失:-)

如果类的实例与类本身具有相同的名称,则可能发生此错误

MyClassSample *MyClassSample = [[MyClassSample alloc] init];
要解决此问题,只需修改实例名称(M->M)


嗯,我的项目导航器只显示一个代表。认为它可能是Delegate.h anywhere的双重include/import。完整的全文搜索在Delegate.m中找到了一个匹配项,该匹配项位于正确的位置。好主意,但最终还不是解决方案-(即使您忘记导入该类别或类。这显示了相同的警告“未找到实例方法“-methodname”(返回类型默认为“id”)”,我确实理解。但它不是“特定的AppDelegateClass”。方法委托返回id。就像您所说的采用-Protocol的对象。如果我强制转换为**id theDelegate=(id)[thisApp委托],原始问题的警告将保持不变。我找到了(至少一个)解决方案,方法是(据我所知)在使用id作为返回类型时向编译器提供较少的信息。id theDelegate=[此应用程序代理];请参阅下面我的答案…您的对象属于您的特定类。这就是您希望能够访问该特定类的属性的原因。请参阅我对您的答案的评论。我认为这根本不是解决此问题的好方法。这不是正确答案。“未找到”仅在缺少声明时发生。例如,他没有导入header@noone,这个答案(上)是正确的。您已将DelegateObject声明为类型id,而该类型没有iVarModel或getAppModel方法。因此,出现警告。若要修复此问题,您必须将其声明为类型AppDelegate*,这肯定是您想要的类。(是的,您需要将赋值类型转换为它,因为委托方法的返回类型更一般。)哦,不,这不是你想要的处理方法。编译器没有混淆。当类型只是id时,编译器会放松这些警告。但是如果你在任何地方都这样做,你会丢失你想要的警告。比如,现在试着拼错一个方法名。你现在正在做的是让编译器蒙在鼓里,正确的答案是gi向编译器提供更多信息,而不是更少。您有一个实现UIApplicationLegate协议的对象。您需要弄清楚为什么编译器认为您的对象不会响应“委托”消息,而不仅仅是扼杀错误。
MyClassSample *MyClassSample = [[MyClassSample alloc] init];
MyClassSample *myClassSample = [[MyClassSample alloc] init];