有条件地在Swift中基于iOS版本导入框架(例如语音)吗? [英] Conditionally import a framework (such as Speech) based on iOS Version in Swift?

查看:48
本文介绍了有条件地在Swift中基于iOS版本导入框架(例如语音)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以根据运行时iOS版本有条件地在Swift中导入框架吗?

Is there any way to conditionally import a framework in Swift based on runtime iOS version?

特别是,我有一个部署目标为iOS 8的应用程序.如果运行时可用,我想在应用程序中使用新的Apple Speech框架.我知道代码段的#available(iOS 10, *)指令,以及可以用于整个类的@available(iOS 10, *)指令.因此,我可以轻松地避免执行任何使用Speech Framework的代码.但是在包含使用Speech类的文件中,我需要"import Speech"语句,并且在那两个指令中都不能使用.我发现即使我的整个班级都有@available(iOS 10, *)指令,当我在iOS 9设备上运行我的应用程序时,它也会在

In particular, I have an app with a deployment target of iOS 8. I would like to use the new Apple Speech framework in the app, if it is available at runtime. I am aware of the #available(iOS 10, *) directive for code sections, and the @available(iOS 10, *) directive which can be used for an entire class. So I can easily avoid executing any code that uses the Speech framework with those. But in the file that contains the class that uses Speech, I need the "import Speech" statement, and neither of those two directives can be used there. I am finding that even if I have the @available(iOS 10, *) directive on my entire class, when I run my app on an iOS 9 device it is killed at launch with

"dyld: Library not loaded: /System/Library/Frameworks/Speech.framework/Speech".

我是否缺少某些东西,还是只能在部署目标为10的应用程序中使用Speech框架?

Am I missing something, or is it only possible to use the Speech framework in an app that has deployment target of 10?

推荐答案

您可以将框架设为可选(详细信息和图像来自

You can make the Framework optional (details and image from Ray Wenderlicht):

这与您使用@available相结合,应该可以防止系统尝试将其加载到不可用的设备上.

This, combined with your use of @available, should prevent the system from trying to load it on the devices where it is not available.

这篇关于有条件地在Swift中基于iOS版本导入框架(例如语音)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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