How do I write universal swift code for both iOS a

2019-06-15 09:30发布

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条回答
叛逆
2楼-- · 2019-06-15 09:49

Use:

#if os(OSX)
    import Cocoa
#elseif os(iOS)
    import UIKit
#endif
查看更多
登录 后发表回答