Created
November 2, 2023 23:00
-
-
Save StewartLynch/cd2747f09d7bb1c53584d4beb6fc54dc to your computer and use it in GitHub Desktop.
Configuration Key Value enum
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
| // Code from NSHipster: https://nshipster.com/xcconfig/ | |
| import Foundation | |
| enum Configuration { | |
| enum Error: Swift.Error { | |
| case missingKey, invalidValue | |
| } | |
| static func value<T>(for key: String) throws -> T where T: LosslessStringConvertible { | |
| guard let object = Bundle.main.object(forInfoDictionaryKey:key) else { | |
| throw Error.missingKey | |
| } | |
| switch object { | |
| case let value as T: | |
| return value | |
| case let string as String: | |
| guard let value = T(string) else { fallthrough } | |
| return value | |
| default: | |
| throw Error.invalidValue | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment