ios - 在 Swift 中匀场

标签 ios macos swift

根据 Apple 工程师 Elizabeth Reid 的说法,“shimming”是指您使用条件编译在 iOS 和 OS X 之间重用代码。例如:

#if TARGET_OS_IPHONE
@import UIKit;
#define BaseView UIView
#else
@import AppKit;
#define BaseView NSView
#endif

@interface MyView : BaseView

@end

在 WWDC 2014 session 中 Sharing code between iOS and OS X她还说:

If you literally translate how you would shim with Objective-C, this will not compile in Swift.

There are ways to shim your code in Swift.

But it gets more complicated than your basic conditional compilation that we can use in Objective-C.

那么,在 Swift 中“填充代码”的方法有哪些?

最佳答案

Swift 中看起来像这样:

#if os(iOS)
    import UIKit
    typealias BaseClass = UIView
    #else
    import AppKit
    typealias BaseClass = NSView
#endif

//

class MyClass : BaseClass {

    // ...

}

关于ios - 在 Swift 中匀场,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41669621/

相关文章:

android - 如何模拟/ stub Flutter 平台 channel /插件?

bash - Mac OS Xargs命令的行为异常

ios - NEHotspotConfigurationErrorDomain Code=5 "invalid EAP settings."

ios - 如何随着时间的推移永久保存文本行

ios - 无法编辑 UITableViewCell y 偏移量

iphone - 图片没有上传到服务器

ios - 获取和更新plist值

svn - OSX 上的 meld(图形差异工具)有哪些替代方案

Swift macOS SegmentedControl Action 没有被调用

ios - 检测何时触摸 Touch ID,无需身份验证