Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift system version checking on Ubuntu

Tags:

swift

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.

like image 328
kmn Avatar asked Dec 14 '22 00:12

kmn


1 Answers

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.

like image 191
Martin R Avatar answered Dec 16 '22 13:12

Martin R