gpt4 book ai didi

ios - 在 Swift 中匀场

转载 作者:IT王子 更新时间:2023-10-29 05:06:40 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com