在Swift中填充 [英] Shimming in Swift

查看:113
本文介绍了在Swift中填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

According to Apple engineer Elizabeth Reid "shimming" is when you use conditional compilation to reuse code between iOS and OS X. For example:

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

@interface MyView : BaseView

@end

在WWDC 2014会议中在iOS和OS X之间共享代码她还声明:

In the WWDC 2014 session Sharing code between iOS and OS X she also states:


如果你真的翻译了如何使用Objective-C进行填充,那么
将无法编译Swift。

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

有很多方法可以在Swift中填充你的代码。

There are ways to shim your code in Swift.

但它比你的基本更复杂我们可以在Objective-C中使用的条件编译

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

那么,这是填充你的代码的方法在Swift?

So, which are the ways to "shim your code" in Swift?

推荐答案

Swift 中看起来像这样:

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

//

class MyClass : BaseClass {

    // ...

}

这篇关于在Swift中填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆