开发者

iPad universal app has method with 'not found in protocol' warning, but it works

开发者 https://www.devze.com 2023-01-05 23:12 出处:网络
My iPad universal app has a method I implemented from here: Best way to programmatically detect iPad/iPhone hardware

My iPad universal app has a method I implemented from here:

Best way to programmatically detect iPad/iPhone hardware

-(BOOL)isPad
{
  BOOL isPad;
  NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
  if(range.location==NSNotFound) isPad=NO;
  else isPad=YES;
  return isPad;
}

When I write my code like this:

开发者_如何学Pythonif( [[[UIApplication sharedApplication] delegate] isPad] ) // do something

I get the warning:

'-isPad' not found in protocol

However, it's declared in my app delegate class:

-(BOOL)isPad;

And in the implementation (above).

Any ideas why this is?

Thanks in advance.


-delegate returns an id<UIApplicationDelegate>. Even if your app delegate supports -isPad, the UIApplicationDelegate does not, which is the warning is about.

You need to cast the return value to your class to eliminate the warning.

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


The compiler expects to find isPad declared in the uiapplicationdelegate protocol. Try making it an instance method of uiapplication instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消