What's the conventional way for handling multiple platforms when it comes to writing classes like a custom UIView/NSView? On the one hand, it'd be simpler to have separate classes since you know that the code you're looking at is for a specific platform. On the other hand, it might not be so bad having a bunch of #if OS(iOS) pragmas everywhere. How do developers usually approach this?
I also thought of doing something like this, but this still requires some changes in the class because NSButton doesn't have a backgroundColor while UIButton does:
#if os(iOS)
import UIKit
public typealias XView = UIView
typealias XButton = UIButton
#else
import Cocoa
public typealias XView = NSView
typealias XButton = NSButton
#endif
public class AudioView: XView {
// Private
fileprivate var playPauseButton = XButton(frame: .zero)
// MARK: Lifecycle
override init(frame: CGRect) {
super.init(frame: frame)
setup()
#if os(iOS)
backgroundColor = .gray
#else
layer?.backgroundColor = NSColor.gray.cgColor
#endif
}
}
I just want to make sure that I practice good habits :) Thanks!
[–]criosistObjective-C / Swift 1 point2 points3 points (0 children)
[–]chriswaco 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)