Skip to content

Instantly share code, notes, and snippets.

@DevAndArtist
Created August 26, 2016 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevAndArtist/a047fe874791ca0bda86d2fc5795ef80 to your computer and use it in GitHub Desktop.
Save DevAndArtist/a047fe874791ca0bda86d2fc5795ef80 to your computer and use it in GitHub Desktop.
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