Ubuntu 上的 Swift 系统版本检查 [英] Swift system version checking on Ubuntu

查看:23
本文介绍了Ubuntu 上的 Swift 系统版本检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Swift 有检查操作系统的预处理器指令:

I know Swift has preprocessor directives that check the OS :

#if os(iOS)
    ...
#elseif os(OSX)
    ...
#endif

但是,在网上搜索之后,我没有发现任何要检查的操作系统是 Ubuntu.有没有办法做到这一点?我知道 swift 最近才开始在 Ubuntu 上工作,所以我意识到在撰写本文时可能没有办法.

But, after searching online, I've found nothing to check is the OS is Ubuntu. Is there a way of doing that ? I know swift has only recently been working on Ubuntu, so I realize that there may not be a way as of this writing.

推荐答案

在 Swift 中,#if ... #endif 不是预处理器语句,而是附上 "条件编译块".os() 平台条件的有效参数被(当前)记录为

In Swift, #if ... #endif are not preprocessor statements, but enclose a "Conditional Compilation Block". The valid arguments for the os() platform condition are (currently) documented as

macOS, iOS, watchOS, tvOS, Linux

因此 #if os(Linux) 检查 Linux 平台.一个典型的例子是

Therefore #if os(Linux) checks for a Linux platform. A typical example is

#if os(Linux)
import Glibc
#else
import Darwin
#endif

从 Linux 和 Apple 平台上的 C 库导入函数.

to import functions from the C library on both Linux and Apple platforms.

这篇关于Ubuntu 上的 Swift 系统版本检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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