How to conditionally compile for tvOS in Swift How to conditionally compile for tvOS in Swift ios ios

How to conditionally compile for tvOS in Swift


#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


This is also covered by Apple under the heading Targeting Apple TV in Your Apps

Listing 1-1 Conditionalizing code for tvOS in Objective-C

 #if TARGET_OS_TV     NSLog(@"Code compiled only when building for tvOS."); #endif

Listing 1-2 Conditionalizing code for tvOS in Swift

#if os(tvOS)NSLog(@"Code compiled only when building for tvOS.");#endifif #available(tvOS 9.1,*) {    print("Code that executes only on tvOS 9.1 or later.")}


#if <build configuration> && !<build configuration>statements#elseif <build configuration>statements#elsestatements#endif

Where build configuration can be :-
os(abc) where valid values for abc are OSX, iOS, watchOS, tvOS, Linux
arch(abc) where valid values for abc are x86_64, arm, arm64, i386

See Apple docs here: