【iOS】系统及设备

旨在快速打通iOS开发流程

By yesmore on 2022-11-28
阅读时间 1 分钟
文章共 245
阅读量

本章内容概览:iOS系统及设备(系统判断、版本兼容等)

上一节:【iOS】05_Swift内置基础库

下一节:【iOS】07_Swift自带属性包装

同系列文章请查看:快乐码元 - iOS篇

1.系统判断

1
2
3
4
5
6
7
#if os(tvOS)
// do something in tvOS
#elseif os(iOS)
// do somthing in iOS
#elseif os(macOS)
// do somthing in macOS
#endif

2.版本兼容

1
2
3
4
5
6
7
8
9
10
11
12
// 版本
@available(iOS 15, *)
func f() {

}

// 版本检查
if #available(iOS 15, macOS 12, *) {
f()
} else {
// nothing happen
}

3.canImport 判断库是否可使用

1
2
3
4
5
#if canImport(SpriteKit)
// iOS 等苹果系统执行
#else
// 非苹果系统
#endif

4.targetEnvironment 环境的判断

1
2
3
4
5
#if targetEnvironment(simulator)
// 模拟器
#else
// 真机
#endif

参考资料:


Tips: Please indicate the source and original author when reprinting or quoting this article.