Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

ios - Swift package manager: How best to indicate platform dependent code?

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.

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) 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) 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...