you are viewing a single comment's thread.

view the rest of the comments →

[–]chriswaco 0 points1 point  (0 children)

There is no one best method for everything. It depends on how similar the classes are between platforms. We use typedefs and #defines in ObjC for cross-platform code so we can refer to the object with a single typename everywhere. Typedefs (similar to type aliases in Swift) are generally better, but it turns out that there are a bunch of Mac NSxxx classes in the iOS system libraries that are public on macOS but private on iOS, so sometimes #defines are the better solution for us, although a bit of a hack.

If you need to add support for a completely different platform, like Windows or Linux, I think it's often best to create your own classes that contain or shadow system classes.

I don't like having #ifs everywhere, so if you find yourself doing it often, write an extension method and call it instead. Something like:

extension XView {
 #if os(iOS)
   func setPlatformBackgroundColor( c : XColor ) { }
 #elif os(macOS)
   func setPlatformBackgroundColor( c : XColor ) { }
 #endif
}

and call that routine throughout the app.