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

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

问题描述

我来了

yld:未找到符号:_OBJC_CLASS_$_UIUserNotificationSettings

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

<块引用>

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

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

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

#if giOS8OrGreater//声明特定于 iOS 8 的函数#别的//声明特定于 iOS 7 的函数#万一

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

解决方案

其他答案没有提到检查系统版本的正确方法.你绝对不应该使用:Device.systemVersion您不应该制作自定义宏来检查版本号,也不应该挖掘 Apple 专门为此任务定义的库.

这里有一篇很棒的文章详细说明了这一点..p>

请注意,Swift 2.0 允许您通过以下方式直接检查操作系统版本号是否可用:

if #available(iOS 10.0, *) {//现代代码} 别的 {//回退到早期版本}

在 Swift 2.0 之前,推荐的方法是通过提供的系统宏:

 if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_9_0) {//为 iOS 9 和更高版本做一些事情} 别的 {//为比 iOS 9 更早的版本做一些事情}

或通过:

 if NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 10, minorVersion: 0, patchVersion: 0)) {//现代代码}

对于系统宏之外缺少的任何内容.

任何其他方法都被轻描淡写为不可靠,Apple 不推荐.实际上有一种方法会在 iOS 10 中打破.

请注意,如果您在检查中需要类似宏的功能并且您想使用 #available 您可以使用 这篇文章这样:

 @available(iOS 7, *)函数 iOS7Work() {//做东西如果#available(iOS 8,*){iOS8Work()}}@available(iOS 8, *)函数 iOS8Work() {//做东西如果 #available(iOS 9, *) {iOS9Work()}}@available(iOS 9, *)func iOS9Work() {//做东西}

有关 Swift 中属性的更多信息,您可以参考 Apple 的文档.

I am getting

yld: Symbol not found: _OBJC_CLASS_$_UIUserNotificationSettings

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()
    }

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.

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

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.

解决方案

The other answers fail to mention proper ways of checking the system version. You should absolutely never utilize: Device.systemVersion You shouldn't make custom macros to check version numbers, and you shouldn't dig beyond the libraries that Apple has specifically defined for this task.

There's a great article detailing this out here.

Note that Swift 2.0 allows you to directly check if an OS version number is available via:

if #available(iOS 10.0, *) {
    // modern code
} else {
    // Fallback on earlier versions
}

Prior to Swift 2.0, the recommended approach was via the system macros provided:

    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_9_0) {
    // do stuff for iOS 9 and newer
} else {
    // do stuff for older versions than iOS 9
}

or via:

    if NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 10, minorVersion: 0, patchVersion: 0)) {
    // modern code
}

For anything missing beyond the system macros.

Any other approach has been downplayed as unreliable and not recommended by Apple. There's actually an approach that will break in iOS 10.

Note that if you need macro like functionality in a check and you'd like to use #available you can use @available defined in this article as such:

 @available(iOS 7, *)
func iOS7Work() {
    // do stuff

    if #available(iOS 8, *) {
        iOS8Work()
    }
}

@available(iOS 8, *)
func iOS8Work() {
    // do stuff
    if #available(iOS 9, *) {
        iOS9Work()
    }
}

@available(iOS 9, *)
func iOS9Work() {
    // do stuff
}

For further information on attributes in Swift, you can reference Apple's documentation.

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

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