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!
Wait for the upcoming release of Swift 5.3. This has been added via SE-0273.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With