根据 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/