Skip to content

Instantly share code, notes, and snippets.

@mergesort
Created February 10, 2023 18:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mergesort/e0d9da4c9123c04ec14733c6ba7e87c9 to your computer and use it in GitHub Desktop.
Save mergesort/e0d9da4c9123c04ec14733c6ba7e87c9 to your computer and use it in GitHub Desktop.
import Foundation
public enum Platform {
case iOS
case iOSOnMac
case mac
}
public func execute(if condition: Bool, _ action: () -> Void) {
if condition {
action()
}
}
public func execute(if platform: Platform, _ action: () -> Void) {
if currentPlatform == platform {
action()
}
}
public func execute(iOS: (() -> Void) = {}, iOSOnMac: () -> Void = {}, mac: () -> Void = {}) {
switch currentPlatform {
case .iOS:
iOS()
case .iOSOnMac:
iOSOnMac()
case .mac:
mac()
}
}
public func execute<P>(iOS: @autoclosure () -> P, iOSOnMac: @autoclosure () -> P, mac: @autoclosure () -> P) -> P {
switch currentPlatform {
case .iOS:
return iOS()
case .iOSOnMac:
return iOSOnMac()
case .mac:
return mac()
}
}
private var currentPlatform: Platform {
#if os(macOS)
return .mac
#else
if ProcessInfo.processInfo.isiOSAppOnMac {
return .iOSOnMac
} else {
return .iOS
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment