我正在开发一个应该能够在 iPad 和 iPhone 上运行的通用应用程序。Apple iPad 文档说用来UI_USER_INTERFACE_IDIOM()
检查我是在 iPad 还是 iPhone 上运行,但我们的 iPhone 是 3.1.2 并且没有UI_USER_INTERFACE_IDIOM()
定义。因此,此代码中断:
//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES; //are we on an iPad?
} else {
return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
}
在 Apple 的SDK 兼容性指南中,他们建议执行以下操作来检查该功能是否存在:
//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(UI_USER_INTERFACE_IDIOM() != NULL &&
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES; //are we on an iPad?
} else {
return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
}
这可行,但会导致编译器警告:“指针和整数之间的比较”。在四处挖掘之后,我发现我可以通过以下转换使编译器警告消失(void *)
:
//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if((void *)UI_USER_INTERFACE_IDIOM() != NULL &&
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES; //are we on an iPad?
} else {
return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
}
我的问题是:这里的最后一个代码块可以/可接受/标准做法吗?我找不到其他人通过快速搜索来做这样的事情,这让我想知道我是否错过了一个陷阱或类似的事情。
谢谢。