From 99e540f80be06afd5b4aaa3d5e5fc949f5ca4fdf Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Mon, 26 Feb 2018 21:33:14 +0100 Subject: [PATCH 1/3] make subscript generic --- Configurations/Configurations.swift | 18 +++++++----------- .../ViewController.swift | 6 ++---- .../ConfigurationsExampleOSX/AppDelegate.swift | 8 ++------ 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/Configurations/Configurations.swift b/Configurations/Configurations.swift index 6c00594..aea1c8d 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 `default` = 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..c09c974 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.default - 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..26cabd4 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.default + self.greetingLabel.stringValue = configuration["Greeting"]! } func applicationWillTerminate(_ aNotification: Notification) { From 31c5767f9dee815993b9b0277436f9048b13191c Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Mon, 26 Feb 2018 21:38:39 +0100 Subject: [PATCH 2/3] s/default/current/ --- Configurations/Configurations.swift | 2 +- .../ConfigurationsExampleIOS/ViewController.swift | 2 +- .../ConfigurationsExampleOSX/AppDelegate.swift | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Configurations/Configurations.swift b/Configurations/Configurations.swift index aea1c8d..ddd698a 100644 --- a/Configurations/Configurations.swift +++ b/Configurations/Configurations.swift @@ -15,7 +15,7 @@ public class Configuration: NSObject { public private(set) var configurationName: String? private var dictionary: NSDictionary! - public static let `default` = Configuration() + public static let current = Configuration() private override init() { super.init() diff --git a/ConfigurationsExample/ConfigurationsExampleIOS/ViewController.swift b/ConfigurationsExample/ConfigurationsExampleIOS/ViewController.swift index c09c974..11ee6e5 100644 --- a/ConfigurationsExample/ConfigurationsExampleIOS/ViewController.swift +++ b/ConfigurationsExample/ConfigurationsExampleIOS/ViewController.swift @@ -16,7 +16,7 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - let configuration = Configuration.default + let configuration = Configuration.current self.greetingLabel.text = configuration["Greeting"]! } diff --git a/ConfigurationsExample/ConfigurationsExampleOSX/AppDelegate.swift b/ConfigurationsExample/ConfigurationsExampleOSX/AppDelegate.swift index 26cabd4..23ad116 100644 --- a/ConfigurationsExample/ConfigurationsExampleOSX/AppDelegate.swift +++ b/ConfigurationsExample/ConfigurationsExampleOSX/AppDelegate.swift @@ -17,7 +17,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ aNotification: Notification) { - let configuration = Configuration.default + let configuration = Configuration.current self.greetingLabel.stringValue = configuration["Greeting"]! } From 5fdfe30fdc882d74fbaa418c5cf6dfdeb0c1e09b Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Mon, 26 Feb 2018 22:23:14 +0100 Subject: [PATCH 3/3] update README --- README.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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