Created
August 26, 2016 18:36
-
-
Save DevAndArtist/a047fe874791ca0bda86d2fc5795ef80 to your computer and use it in GitHub Desktop.
A function to assign device specific value for UIKit.
This file contains hidden or 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
// 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