Swift软件包管理器:如何最好地指示平台相关代码? [英] Swift package manager: How best to indicate platform dependent code?

查看:44
本文介绍了Swift软件包管理器:如何最好地指示平台相关代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift软件包管理器(SPM)允许支持不同的平台(例如,iOS,macOS).我正在改编Swift库以使用SPM,并且需要共享iOS和macOS的一些代码,但也要有一些区别.

The Swift Package Manager (SPM) allows support for different platforms (e.g., iOS, macOS). I'm adapting a Swift library to use SPM, and the need is for some of the code for iOS and macOS to be shared, but to have some differences as well.

我已经在Facebook库中看到了类似的例子: https://github.com/facebook/facebook-ios-sdk/blob/master/Package.swift 在这种情况下,他们使用多个目标,每个目标都有其自己的(独立)代码.这不是平台依赖关系的示例,而是目标依赖关系的示例.

I've seen a similar example of this with the Facebook libraries: https://github.com/facebook/facebook-ios-sdk/blob/master/Package.swift In that case, they use multiple targets, each with its own (independent) code. This is not an example of platform dependency, rather of target dependency.

我看到了两条前进的道路:

I see two paths forward:

1)具有针对iOS和macOS的单独的库目标-并使用与Facebook类似的方法,但是大部分代码将在目标之间共享.我不确定SPM是否允许跨目标共享代码.缺点仅在于语法糖-命名差异.不幸的是,您必须在iOS上输入"MyLibrary_iOS",在macOS上输入"MyLibrary_macOS".

1) Have separate library targets for iOS and macOS-- and use a similar approach to Facebook, but the bulk of the code would be shared across the targets. I'm not entirely sure if SPM allows code shared across targets. A downside here is purely in terms of syntactic sugar-- naming differences. It seems unfortunate that you'd have to import say "MyLibrary_iOS" on iOS and "MyLibrary_macOS" on macOS.

2)有一个用于iOS和macOS的目标,但将条件编译嵌入源代码中以有条件地包括/排除特定文件.这没有上面的命名问题.但是似乎必须执行此条件编译是不干净的.

2) Have a single target for iOS and macOS, but embed conditional compilation within the source code to conditionally include/exclude specific files. This doesn't have the naming issue as above. But it seems unclean to have to do this conditional compilation.

还有其他建议吗?谢谢!

Any other suggestions? Thanks!

推荐答案

请等待即将发布的Swift 5.3.已通过 SE添加-0273 .

Wait for the upcoming release of Swift 5.3. This has been added via SE-0273.

这篇关于Swift软件包管理器:如何最好地指示平台相关代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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