iPad通用应用程序具有';在协议'中找不到;警告,但它是有效的

iPad通用应用程序具有';在协议'中找不到;警告,但它是有效的,ipad,Ipad,我的iPad通用应用程序有一个方法,我在这里实现: 当我这样编写代码时: if( [[[UIApplication sharedApplication] delegate] isPad] ) // do something 我得到警告: 在协议中找不到“-isPad” 但是,它在我的应用程序委托类中声明: -(BOOL)isPad; 在实施过程中(如上) 你知道为什么吗 提前感谢。编译器希望找到UIApplicationLegate协议中声明的isPad。尝试将其作为uiapplicati

我的iPad通用应用程序有一个方法,我在这里实现:

当我这样编写代码时:

if( [[[UIApplication sharedApplication] delegate] isPad] ) // do something
我得到警告:

在协议中找不到“-isPad”

但是,它在我的应用程序委托类中声明:

-(BOOL)isPad;
在实施过程中(如上)

你知道为什么吗


提前感谢。

编译器希望找到UIApplicationLegate协议中声明的isPad。尝试将其作为uiapplication的实例方法。

-delegate
返回一个
id
。即使您的应用程序代理支持
-isPad
,UIApplicationLegate也不支持,这就是警告

您需要将返回值强制转换为类以消除警告

YourAppDelClass* appDel = [UIApplication sharedApplication].delegate;
if ([appDel isPad]) {
   ...

我创建了一个引用变量并使用它来调用该方法,它删除了警告。ThanksIt会更简单,就像我在问题中所做的那样,每次创建一个ref变量都是一件痛苦的事情。是的,我就是这样解决的,通过创建一个ref变量。感谢您的帮助,我能否将其转换为h文件中的define语句?是-#define kIsPad[(MyAppDelegate*)[[UIApplication sharedApplication]delegate]isPad]
YourAppDelClass* appDel = [UIApplication sharedApplication].delegate;
if ([appDel isPad]) {
   ...