Last active
August 29, 2015 14:21
-
-
Save JanGorman/c70c5621712b6e1f7c1b 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
struct Configuration: BooleanType { | |
private let isActiveConfiguration: Bool | |
// Extend to your heart's content, e.g. size classes | |
init(idiom: UIUserInterfaceIdiom? = nil, os: NSOperatingSystemVersion? = nil) { | |
var configuration = true | |
if let idiom = idiom { | |
configuration = idiom == UIDevice.currentDevice().userInterfaceIdiom | |
} | |
if let os = os { | |
configuration = configuration && NSProcessInfo().operatingSystemVersion == os | |
} | |
isActiveConfiguration = configuration | |
} | |
var boolValue: Bool { | |
return isActiveConfiguration | |
} | |
} | |
extension NSOperatingSystemVersion: Equatable {} | |
public func ==(lhs: NSOperatingSystemVersion, rhs: NSOperatingSystemVersion) -> Bool { | |
return lhs.majorVersion == rhs.majorVersion && lhs.minorVersion == rhs.minorVersion | |
&& lhs.patchVersion == rhs.patchVersion | |
} | |
// Use like so: | |
if Configuration(idiom: .Pad, os: NSOperatingSystemVersion(majorVersion: 8, minorVersion: 3, patchVersion: 0)) { | |
println("I'm an iPad running 8.3") | |
// Do something useful | |
} | |
if Configuration(idiom: .Phone) { | |
println("I'm any iPhone") | |
} | |
// OR: as function with a closure | |
func configuration(idiom: UIUserInterfaceIdiom? = nil, atLeast: NSOperatingSystemVersion? = nil, then: () -> Void) { | |
var configuration = true | |
if let idiom = idiom { | |
configuration = idiom == UIDevice.currentDevice().userInterfaceIdiom | |
} | |
if let atLeast = atLeast { | |
configuration = configuration && NSProcessInfo().isOperatingSystemAtLeastVersion(atLeast) | |
} | |
if configuration { | |
then() | |
} | |
} | |
// Usage: | |
configuration(idiom: .Phone, atLeast: NSOperatingSystemVersion(majorVersion: 8, minorVersion: 0, patchVersion: 0)) { | |
println("I'm a phone running at least iOS8") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment