Shared code for iOS and OS X

Hello guys, so I wanted to create one file with one class and share the code in both iOS and OS X app. The code (Swift):


#if !TARGET_OS_IPHONE
    import Cocoa
    public typealias Controller1 = NSViewController
#else   
    import UIKit
    public typealias Controller1 = UIViewController
#endif


And when I try to build it I get "No such module Cocoa" and it's true, iOS have no Cocoa module. Is there any way to create a platform-depended class?

Answered by Nils in 88113022

After changing code to

#if os(iOS)
    import UIKit
    public typealias Controller1 = UIViewController   
#else
    import Cocoa
    public typealias Controller1 = NSViewController
#endif

it builds without any error :-)

Accepted Answer

After changing code to

#if os(iOS)
    import UIKit
    public typealias Controller1 = UIViewController   
#else
    import Cocoa
    public typealias Controller1 = NSViewController
#endif

it builds without any error :-)

Shared code for iOS and OS X
 
 
Q