kmn
kmn

Reputation: 2655

Swift system version checking on Ubuntu

I know Swift has preprocessor directives that check the OS :

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

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.

Upvotes: 6

Views: 2832

Answers (1)

Martin R
Martin R

Reputation: 540105

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

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

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

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

Upvotes: 16

Related Questions