iosswifttvos

How to conditionally compile for tvOS in Swift


How do I conditionally compile code for iOS and tvOS in the same file in the Swift language? I have tried all the Objective-C style #if etc. for TARGET_OS_TV as mentioned in the Apple docs, and some other answers. But I have not found a working solution for Swift code.


Solution

  • #if os(OSX)
    // compiles for OS X
    #elseif os(iOS)
    // compiles for iOS
    #elseif os(tvOS)
    // compiles for TV OS
    #elseif os(watchOS)
    // compiles for Apple watch
    #endif