5

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.

kmn
  • 2,615
  • 4
  • 18
  • 28
  • `#if os(Linux)` – Example here: http://stackoverflow.com/questions/41035180/swift-arc4random-uniformmax-in-linux. – Martin R Mar 03 '17 at 21:47
  • 1
    And documented here: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/doc/uid/TP40014097-CH33-ID538. – Martin R Mar 03 '17 at 21:51
  • @MartinR ,Thank you do you want to write an answer So I can accepted ? – kmn Mar 03 '17 at 21:51

1 Answers1

14

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382