Skip to content

Instantly share code, notes, and snippets.

@davidsteppenbeck
Last active December 25, 2023 22:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsteppenbeck/b7601bd79db234af018b20d17b0689f2 to your computer and use it in GitHub Desktop.
Save davidsteppenbeck/b7601bd79db234af018b20d17b0689f2 to your computer and use it in GitHub Desktop.
A global function for providing multiplatform specific values in Swift.
import Foundation
/// Returns the value for the current platform from the values that are provided.
///
/// - Parameters:
/// - iOSValue: The value to use for the iOS platform.
/// - macOSValue: The value to use for the macOS platform.
func valueForPlatform<Value>(iOS iOSValue: @autoclosure () -> Value, macOS macOSValue: @autoclosure () -> Value) -> Value {
#if os(iOS)
iOSValue()
#elseif os(macOS)
macOSValue()
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment