什么是Swift预处理器相当于iOS版本检查比较? [英] What is the Swift preprocessor equivalent to iOS version check comparison?

查看:80
本文介绍了什么是Swift预处理器相当于iOS版本检查比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在

yld: Symbol not found: _OBJC_CLASS_$_UIUserNotificationSettings

这是当应用程序在iOS7设备上运行时导致错误 的函数 甚至没有在代码中完全调用该函数。

and here's the function that is causing the error when the application is running on an iOS7 device and without even calling the function at all in the code.


func reigsterForRemoteUserNotifications(notificationTypes: UIUserNotificationType, categories: NSSet) {
        let userNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categories)
        (UIApplication.sharedApplication()).registerUserNotificationSettings(userNotificationSettings)
        UIApplication.sharedApplication().registerForRemoteNotifications()
    }


我不想要这种方法在iOS7设备上运行时可以访问。我不希望在其中进行选择检查,因为这意味着该方法可用于开始。

I don't want this method to be accessible at all when running on an iOS7 device. I do not want a select check inside of it because that means the method is available for use to begin with.

我想要的是用于检查版本的构建配置参数:
我无法找到一种方法来编写一个快速等效的预处理器宏来检查正确的iOS版本并忽略新的和未声明的iOS 8库函数。

What I want is a build config parameter to check the version : I can't figure out a way to write a swift equivalent preprocessor macro to check for the correct iOS version and neglect the new and undeclared iOS 8 library functions.

#if giOS8OrGreater
// declare the functions that are iOS 8 specific
#else 
// declare the functions that are iOS 7 specific

#endif

在文档中,apple建议使用函数和泛型来替换复杂的宏,但在这种情况下,我需要构建配置预编译检查以避免处理未声明的函数。任何建议。

In the documentation apple is suggesting functions and generics to substitute for complex macros but in this case I need a build config precompile check to avoid processing undeclared functions. Any suggestions.

推荐答案

Constants.swift 中:

Swift 1:

let Device = UIDevice.currentDevice()
private let iosVersion = NSString(string: Device.systemVersion).doubleValue

Swift 2:

let Device = UIDevice.currentDevice()
private let iosVersion = Double(Device.systemVersion) ?? 0 

let iOS8 = iosVersion >= 8
let iOS7 = iosVersion >= 7 && iosVersion < 8

然后在其他文件中

if iOS8
{

}
else
{

}

这篇关于什么是Swift预处理器相当于iOS版本检查比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆