Objective-c 中的条件注释标签

Posted

技术标签:

【中文标题】Objective-c 中的条件注释标签【英文标题】:Conditional comment tags in Objective-c 【发布时间】:2014-06-23 19:48:37 【问题描述】:

抱歉,这似乎是一个愚蠢的问题:

我目前正在为 ios8 构建 phonegap 应用程序,唯一的问题是 iOS8 弃用了一些用于 Pus Notifications 的代码,所以在 iOS8 中我已经让它们工作,但我用来工作的代码在 iOS7 中不起作用,我试图让它对两者都有效。我使用 if else 语句来检查版本是否正常,但问题是即使 if else 只会触发正确版本的代码,但在 iOS7 中,我的应用程序不会正确编译,因为它会看到不存在的较新代码iOS7 SDK,有什么方法可以使这个工作吗?就像您如何在 html 中为特定浏览器版本执行条件 cmets?

这就是我所拥有的,如您所见,它只是一个 if else 语句,但显然它不适用于 iOS7 SDK,因为该 SDK 不知道初始 (if) 中的代码是什么,因为它在 iOS8 中是新的

NSArray *vComp = [[UIDevice currentDevice].systemVersion   componentsSeparatedByString:@"."];

if ([[vComp objectAtIndex:0] intValue] >= 8) 

    /// First register notification setting with settings type like
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications]; // you can also set here for local notification.

 else if ([[vComp objectAtIndex:0] intValue] < 8) 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];

【问题讨论】:

请发布您的尝试。提示:objective C 几乎是 C 的超集,包括像 #ifdef 这样的预处理器指令。 【参考方案1】:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
//Put your iOS 8 code here
#else
// Put iOS 7 and below here
#endif

【讨论】:

以上是关于Objective-c 中的条件注释标签的主要内容,如果未能解决你的问题,请参考以下文章

从 javascript 到 Objective-C 的通信

编程基础 - 注释标签说明

根据单元格OBjective-C中的标签文本(动态)增加单元格高度

如何根据objective-C中的标签号将按钮添加到MutableArray?

iOS本地化在Xcode中不适用于Objective-C中的自定义标签[关闭]

如何在 Objective-C 中的地图上拖放和拖动图钉