问题
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
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
回答1:
Use:
#if os(OSX)
import Cocoa
#elseif os(iOS)
import UIKit
#endif
来源:https://stackoverflow.com/questions/26339700/how-do-i-write-universal-swift-code-for-both-ios-and-os-x-in-cocoa-i-could-use