How to perform platform check with typealias and @available

Issue #38

The other day, I was building Anchors which needs to support iOS and macOS. What’s clever way to not use #if os(iOS) || os(tvOS) in all files? Use typealias

This is the first version. I’m trying to support iOS 8, macOS 10.10

1
2
3
4
5
6
7
8
9
10
#if os(iOS) || os(tvOS)
import UIKit
public typealias View = UIView
public typealias LayoutGuide = UILayoutGuide
public typealias EdgeInsets = UIEdgeInsets
#elseif os(OSX)
import AppKit
public typealias View = NSView
public typealias LayoutGuide = NSLayoutGuide
#endif

But then because of LayoutGuide, I need to bump deployment target to iOS 9, macOS 10.11. Which is not what I want. @available to the rescue, but it will affect everything below it. The solution is to split the platform check, the first as normal, the second with @available check

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#if os(iOS) || os(tvOS)
import UIKit
public typealias View = UIView
public typealias EdgeInsets = UIEdgeInsets
#elseif os(OSX)
import AppKit
public typealias View = NSView
#endif

#if os(iOS) || os(tvOS)
import UIKit
@available(iOS 9.0, *)
public typealias LayoutGuide = UILayoutGuide
#elseif os(OSX)
import AppKit
@available(macOS 10.11, *)
public typealias LayoutGuide = NSLayoutGuide
#endif

Comments