Skip to content

Generics #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions Configurations/Configurations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand All @@ -40,9 +36,9 @@ public class Configuration: NSObject {
self.dictionary = dictionary?.value(forKey: self.configurationName!) as? NSDictionary
}

public subscript(key: String) -> AnyObject? {
public subscript<T>(key: String) -> T? {
get {
return self.dictionary.value(forKey: key) as AnyObject
return self.dictionary.value(forKey: key) as? T
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down