I am currently building an iOS app with a watchOS app extension. I have certain classes that are shared between both apps, and they are largely identical on both platforms, but with certain variations. I am wondering how this is best implemented. Right now I use the following:
#if os(watchOS)
private var watchOSOnlyProperty: Any?
#endif
or
#if os(iOS)
func iOSOnlyMethod() {
...
}
#endif
While this works, it gives several problems since XCode seems to be pretty confused about this. Indentation, autocompletion and the list of methods are all a bit off when using this syntax.
What is the proper way to do this? How can you add iOS/watchOS-specific properties & methods to shared classes in Swift?
Code separation
There are two ways to separate code
#if os(...) like you suggested.Auto completion
Xcode will only autocomplete code for your current selected target.

If you want autocompletion for code inside #if os(watchOS) blocks then you need to change your current target (via the toolbar) to the wathOS target.
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