版本特定的预处理器宏
我有一个预处理器命令,用于检测 iOS 版本以及是否支持 iCloud。我想知道是否像这样的宏:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 50000
//stuff
#endif
如果使用 iOS 4.x 的人从应用程序商店下载该应用程序但它是在 iOS 5.x 上编译的,这是否会起作用。
由于这将在编译时进行评估,因此应用程序是在设备上编译的还是如何工作的?有没有更好的方法来达到相同的结果?
I have a preprocessor command for detecting the version of iOS and supporting iCloud or not. What I am wondering is if a macro like so:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 50000
//stuff
#endif
will this work if someone with iOS 4.x downloads the app from the app store but it was compiled on/for iOS 5.x.
Since this would be evaluated on compile time are the apps compiled on the device or how does that work? Is there a better way for the same result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能为此使用宏。宏在编译时评估,而不是在运行时评估,这正是您想要进行功能/功能检测(例如 iCloud 支持)的情况。 (否则你最终会启用它,因为在所有情况下你都是针对 iOS 5 SDK 进行编译的)
你应该只测试是否存在普遍存在的方法,它告诉你是否可以调用它们告诉您是否支持 iCloud,例如:
快速后续注释回复:您关于编译的问题。编译是将实际代码转换为在设备上运行的二进制文件的过程。当您在 Xcode 中进行 Build 时,就会发生这种情况,并且它仅发生在您的计算机上,而不会发生在 Apple 或设备上。这就是为什么编译时检查版本号不起作用的原因——当您将其发送到设备时,决定已经做出了。
You can't use macros for this. Macros are evaluated at compile time, and not at runtime, which is what you want for feature/capability detection, like iCloud support. (Otherwise you'll end up with it enabled since you're compiling against the iOS 5 SDK in all cases)
You should just test for the presence of the ubiquity methods, which tell you whether you can call them, which tells you whether iCloud is supported, e.g.:
Quick followup note re: your question about compiling. Compilation is the process that turns your actual code into the binary that runs on the device. This happens when you do Build in Xcode, and it happens on your machine only, never at Apple or on the device. This is why compile-time checks for version numbers won't work-- by the time you send it to a device, the decision would have already been made.