How do I write universal swift code for both iOS and OS X. In cocoa I could use #ifdef, what do I do now?

for our project we always used one source file for both platforms: iOS and OS X. Right now I am migrating to swift. Unfortunately there is some files which need

import Cocoa

and on iOS

  • Cannot import UIKit
  • Swift REPL: how to import, load, evaluate, or require a .swift file?
  • How can I access variables from another class?
  • ignore ios8 code in xcode 5 compilation
  • Can't import main swift classes into test target?
  • Errors when making circular reference imports
  • import UIKit
    

    previously we did

    #ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
    #import <Cocoa/Cocoa.h>
    #else
    #import <UIKit/UIKit.h>
    #endif
    

    Do you know how this can be done in swift? I don’t like to write each class twice just because there is no macros anymore.

    Thanks in advance

    Jack

    Solutions Collect From Internet About “How do I write universal swift code for both iOS and OS X. In cocoa I could use #ifdef, what do I do now?”

    Use:

    #if os(OSX)
        import Cocoa
    #elseif os(iOS)
        import UIKit
    #endif