Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ykyouhei committed Jul 7, 2016
1 parent c4841bd commit 099f98d
Showing 4 changed files with 106 additions and 15 deletions.
81 changes: 80 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
# SwiftyConfiguration
# SwiftyConfiguration

[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![Pod Version](http://img.shields.io/cocoapods/v/SwiftyConfiguration.svg?style=flat)](http://cocoadocs.org/docsets/SwiftyConfiguration/)
[![Pod Platform](http://img.shields.io/cocoapods/p/SwiftyConfiguration.svg?style=flat)](http://cocoadocs.org/docsets/SwiftyConfiguration/)
[![Pod License](http://img.shields.io/cocoapods/l/SwiftyConfiguration.svg?style=flat)](https://github.com/ykyohei/SwiftyConfiguration/blob/master/LICENSE)
![Swift version](https://img.shields.io/badge/swift-2.2-orange.svg)

SwiftyConfiguration is modern Swift API for Plist.


## Usage

##### 1. Create Plist
create plist file and add your project.

![plist.png](./Images/plist.png "plist.png")

##### 2. Define your plist keys
```Swift
extension Keys {
static let string = Key<String>("string")
static let url = Key<NSURL>("url")
static let number = Key<NSNumber>("number")
static let int = Key<Int>("int")
static let float = Key<Float>("float")
static let double = Key<Double>("double")
static let date = Key<NSDate>("date")
static let bool = Key<Bool>("bool")
static let array = Key<Array<AnyObject>>("array")
static let dictionary = Key<[String : String]>("dictionary")

// Support nested value.
static let innerInt = Key<Int>("array.0")
static let innerArray = Key<String>("array.1.0")
static let innerString = Key<String>("dictionary.innerString")
}
```

##### 3. Just use it!
```Swift
let plistPath = NSBundle.mainBundle().pathForResource("Configuration", ofType: "plist")!
let config = Configuration(plistPath: plistPath)!

// You can get a plist value to type-safe.
let stringValue = config.get(.string)!
let urlValue = config.get(.url)!
let numberValue = config.get(.number)!
let intValue = config.get(.int)!
let floatValue = config.get(.float)!
let doubleValue = config.get(.double)!
let dateValue = config.get(.date)!
let boolValue = config.get(.bool)!
let arrayValue = config.get(.array)!
let dictionaryValue = config.get(.dictionary)!
let innerIntValue = config.get(.innerInt)!
let innerArrayValue = config.get(.innerArray)!
let innerStringValue = config.get(.innerString)!
```

## Installation

### Carthage
`SwiftyConfiguration` is available through [Carthage](https://github.com/Carthage/Carthage). To install it, simply add the following line to your Cartfile:

```ruby
github "ykyouhei/SwiftyConfiguration"
```

### CocoaPods

`SwiftyConfiguration` is available on CocoaPods.
Add the following to your `Podfile`:

```ruby
pod 'SwiftyConfiguration'
```

### Manually
Just add the Classes folder to your project.
6 changes: 5 additions & 1 deletion SwiftyConfiguration/Classes/Configuration.swift
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import Foundation
public protocol PlistValueType {}

extension String: PlistValueType {}
extension NSURL: PlistValueType {}
extension NSNumber: PlistValueType {}
extension Int: PlistValueType {}
extension Float: PlistValueType {}
@@ -21,9 +22,12 @@ extension NSData: PlistValueType {}
extension Array: PlistValueType {}
extension Dictionary: PlistValueType {}

/// Extend this class and add your plist keys as static constants
/// so you can use the shortcut dot notation (e.g. ` configuration.get(.yourKey)`)

public class Keys {}

public class Key<ValueType: PlistValueType>: Keys {
public final class Key<ValueType: PlistValueType>: Keys {

public let key: String

28 changes: 16 additions & 12 deletions SwiftyConfigurationTests/Configuration.plist
Original file line number Diff line number Diff line change
@@ -2,20 +2,12 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>dictionary</key>
<dict>
<key>innerString</key>
<string>moge</string>
</dict>
<key>array</key>
<array>
<integer>0</integer>
<array>
<string>array.1.0</string>
</array>
</array>
<key>string</key>
<string>hoge</string>
<key>url</key>
<string>https://github.com/ykyouhei/SwiftyConfiguration</string>
<key>number</key>
<integer>0</integer>
<key>int</key>
<integer>1</integer>
<key>float</key>
@@ -26,5 +18,17 @@
<true/>
<key>date</key>
<date>1970-01-01T00:00:00Z</date>
<key>dictionary</key>
<dict>
<key>innerString</key>
<string>moge</string>
</dict>
<key>array</key>
<array>
<integer>0</integer>
<array>
<string>array.1.0</string>
</array>
</array>
</dict>
</plist>
6 changes: 5 additions & 1 deletion SwiftyConfigurationTests/SwiftyConfiguration.swift
Original file line number Diff line number Diff line change
@@ -12,13 +12,15 @@ import XCTest
private extension Keys {

static let string = Key<String>("string")
static let url = Key<NSURL>("url")
static let number = Key<NSNumber>("number")
static let int = Key<Int>("int")
static let float = Key<Float>("float")
static let double = Key<Double>("double")
static let date = Key<NSDate>("date")
static let bool = Key<Bool>("bool")

static let array = Key<Array<NSObject>>("array")
static let array = Key<Array<AnyObject>>("array")
static let innerInt = Key<Int>("array.0")
static let innerArray = Key<String>("array.1.0")

@@ -45,6 +47,8 @@ class SwiftyConfigurationTests: XCTestCase {
let config = Configuration(plistPath: plistPath)!

XCTAssertTrue("hoge" == config.get(.string)!)
XCTAssertTrue(NSURL(string: "https://github.com/ykyouhei/SwiftyConfiguration")! == config.get(.url)!)
XCTAssertTrue(NSNumber(int: 0) == config.get(.number)!)
XCTAssertTrue(1 == config.get(.int)!)
XCTAssertTrue(1.1 == config.get(.float)!)
XCTAssertTrue(3.14 == config.get(.double)!)

0 comments on commit 099f98d

Please sign in to comment.