The title may seem a little confusing, but consider this scenario. We have the following extensions on UIView for iOS/iPadOS apps.
import UIKit
extension UIView {
var isVisible:Bool{
get{ return !isHidden }
set{ isHidden = !newValue }
}
func addSubviews(_ subviews:UIView...) {
addSubviews(subviews)
}
func addSubviews(_ subviews:[UIView]) {
subviews.forEach{ addSubview($0) }
}
}
We have the exact same extensions on NSView for macOS apps.
import AppKit
extension NSView {
var isVisible:Bool{
get{ return !isHidden }
set{ isHidden = !newValue }
}
func addSubviews(_ subviews: NSView...) {
addSubviews(subviews)
}
func addSubviews(_ subviews: [NSView]) {
subviews.forEach{ addSubview($0) }
}
}
The only difference between the two is one uses UIView, the other uses NSView.
To get rid of that redundancy, and to eliminate having to add the platform checks everywhere, we simply aliased the class names themselves, then built the APIs around those aliases, like so...
#if os(iOS) || os(tvOS)
import UIKit
typealias PlatformView = UIView
#elseif os(OSX)
import AppKit
typealias PlatformView = NSView
#endif
extension PlatformView {
var isVisible:Bool{
get{ return !isHidden }
set{ isHidden = !newValue }
}
func addSubviews(_ subviews: PlatformView...) {
addSubviews(subviews)
}
func addSubviews(_ subviews:[PlatformView]) {
subviews.forEach{ addSubview($0) }
}
The issue is we now have to expose PlatformView publicly, which may confuse those importing our Swift package. We're hoping they would just see either UIView or NSView depending on their platform.
So... is that possible to hide your type aliases and instead expose the public types they alias?