jessesquires / jessesquires.com

Turing complete with a stack of 0xdeadbeef
https://www.jessesquires.com
Other
46 stars 19 forks source link

Improving multiplatform SwiftUI code #187

Closed qupig closed 1 year ago

qupig commented 1 year ago

Comments, Questions, Feedback: https://www.jessesquires.com/blog/2023/03/23/improve-multiplatform-swiftui-code/

This approach really makes the code look neat.

But at the same time, we may lose the advantage that #if os() brings to us, that is, the compilation process omits code that is not related to the specific platform.

In your example, #if os() is used to change the self pointer, and all configuration code will be flooded to each platform.

jessesquires commented 1 year ago

Thanks @qupig! 😄

Sure, that's a valid concern in some cases. In that situation, you can also wrap the properties in #if:

#if os(iOS)
private var body_iOS: some View {
    // the body for iOS only
}
#endif 

#if os(macOS)
private var body_macOS: some View {
    // the body for macOS only
}
#endif 
jessesquires commented 1 year ago

It's also worth pointing out that the compiler may (very likely) strip dead symbols also.

So for example, body_macOS (without the #if) might be removed by the compiler when building for iOS.

github-actions[bot] commented 1 year ago

Thanks so much for leaving feedback about this blog post! After 2 weeks of inactivity these issues are automatically closed, but feel free to continue the discussion!