diff --git a/Configurations/Configurations.swift b/Configurations/Configurations.swift index 6c00594..ddd698a 100644 --- a/Configurations/Configurations.swift +++ b/Configurations/Configurations.swift @@ -14,14 +14,9 @@ public class Configuration: NSObject { public private(set) var configurationName: String? private var dictionary: NSDictionary! - - public static func defaultConfiguration() -> Configuration { - struct Static { - static var instance = Configuration() - } - return Static.instance - } - + + public static let current = Configuration() + private override init() { super.init() @@ -30,7 +25,8 @@ public class Configuration: NSObject { self.configurationName = bundle.infoDictionary![CurrentConfigurationPlistKey] as? String guard self.configurationName != nil else { - fatalError("No Configuration property found in plist") + assertionFailure("No Configuration property found in plist") + return } let plistName = bundle.infoDictionary![self.ConfigurationPlistKey] as! String @@ -40,9 +36,9 @@ public class Configuration: NSObject { self.dictionary = dictionary?.value(forKey: self.configurationName!) as? NSDictionary } - public subscript(key: String) -> AnyObject? { + public subscript(key: String) -> T? { get { - return self.dictionary.value(forKey: key) as AnyObject + return self.dictionary.value(forKey: key) as? T } } diff --git a/ConfigurationsExample/ConfigurationsExampleIOS/ViewController.swift b/ConfigurationsExample/ConfigurationsExampleIOS/ViewController.swift index 746fc7f..11ee6e5 100644 --- a/ConfigurationsExample/ConfigurationsExampleIOS/ViewController.swift +++ b/ConfigurationsExample/ConfigurationsExampleIOS/ViewController.swift @@ -16,11 +16,9 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - let configuration = Configuration.defaultConfiguration() + let configuration = Configuration.current - if let value = configuration["Greeting"] as? String { - self.greetingLabel.text = value - } + self.greetingLabel.text = configuration["Greeting"]! } override func didReceiveMemoryWarning() { diff --git a/ConfigurationsExample/ConfigurationsExampleOSX/AppDelegate.swift b/ConfigurationsExample/ConfigurationsExampleOSX/AppDelegate.swift index 9d3ef38..23ad116 100644 --- a/ConfigurationsExample/ConfigurationsExampleOSX/AppDelegate.swift +++ b/ConfigurationsExample/ConfigurationsExampleOSX/AppDelegate.swift @@ -15,15 +15,11 @@ class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! @IBOutlet weak var greetingLabel: NSTextField! - func applicationDidFinishLaunching(_ aNotification: Notification) { - let configuration = Configuration.defaultConfiguration() - - if let value = configuration["Greeting"] as? String { - self.greetingLabel.stringValue = value - } + let configuration = Configuration.current + self.greetingLabel.stringValue = configuration["Greeting"]! } func applicationWillTerminate(_ aNotification: Notification) { diff --git a/README.md b/README.md index 1d45849..9bdc133 100644 --- a/README.md +++ b/README.md @@ -30,15 +30,28 @@ Import the framework: import Configurations ``` -And get the default configuration: +And get the current configuration: ```swift -let configuration = Configuration.defaultConfiguration() +let configuration = Configuration.current if let someValue = configuration["someKey"] as? String { print("someKey: \(someValue)") } ``` +By making use of generics the return type of a value can be inferred from the +receiver. + +For example; the `text` property of a `UILabel` expects a `String`; + +```swift +let configuration = Configuration.current +let label = UILabel() +label.text = configuration["someKey"]! +``` + +But do make sure that the value in the plist is of a matching type. + ## Contributing 1. Fork it