Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A function to assign device specific value for UIKit.
// Swift 3.0:
// This functuion makes use of the custom operator `?!`:
// https://gist.github.com/DevAndArtist/dad641ee833e60b02fd1db2dbb488c6a
@available(iOS 9.0, *)
func forDevice<T>(
phone: @autoclosure () -> T? = nil,
pad: @autoclosure () -> T? = nil,
tv: @autoclosure () -> T? = nil,
carPlay: @autoclosure () -> T? = nil) -> T {
let idiom = UIDevice.current.userInterfaceIdiom
switch idiom {
case .phone:
return phone() ?! fatalError("`phone` unspecified")
case .pad:
return pad() ?! fatalError("`pad` unspecified")
case .tv:
return tv() ?! fatalError("`tv` unspecified")
case .carPlay:
return carPlay() ?! fatalError("`carPlay` unspecified")
default:
fatalError()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment