Skip to content

Instantly share code, notes, and snippets.

@StewartLynch
Created November 2, 2023 23:00
Show Gist options
  • Select an option

  • Save StewartLynch/cd2747f09d7bb1c53584d4beb6fc54dc to your computer and use it in GitHub Desktop.

Select an option

Save StewartLynch/cd2747f09d7bb1c53584d4beb6fc54dc to your computer and use it in GitHub Desktop.
Configuration Key Value enum
// 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