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

给你一囗甜甜゛ 提交于 2019-12-23 06:57:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!