Last active
October 15, 2023 12:16
-
-
Save ra9r/211422c270ce091738a6cec6f9a785ce to your computer and use it in GitHub Desktop.
Enum wrapper for easier info.pist access
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
// | |
// Configuration.swift | |
// Configurations and Flags | |
// | |
// Created by Stewart Lynch on 2021-09-27. | |
// | |
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
Taken from this GitHub project and is part of a video by StewartLynch.