Created
February 10, 2023 18:51
-
-
Save mergesort/e0d9da4c9123c04ec14733c6ba7e87c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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