Skip to content

Instantly share code, notes, and snippets.

@ra9r
Last active October 15, 2023 12:16
Show Gist options
  • Save ra9r/211422c270ce091738a6cec6f9a785ce to your computer and use it in GitHub Desktop.
Save ra9r/211422c270ce091738a6cec6f9a785ce to your computer and use it in GitHub Desktop.
Enum wrapper for easier info.pist access
//
// 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
}
}
}
@ra9r
Copy link
Author

ra9r commented Nov 1, 2022

Taken from this GitHub project and is part of a video by StewartLynch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment