A simple Swift library to work with plist files and data. Inspired by SwitfyJSON.
import Plist
// initialize using an NSDictionary
// and retrieve keyed values
let info = Plist(dict)
let name = info["name"].string ?? ""
let age = info["age"].int ?? 0
// initialize using an NSArray
// and retrieve indexed values
let info = Plist(array)
let itemAtIndex0 = info[0].value
// load an applications info.plist data
let info = Plist(NSBundle.mainBundle().infoDictionary)
let identifier = info["CFBundleIndentifier"].string!
// utility initiaizer to load a plist file at specified path
let info = Plist(path: "path_to_plist_file")
// we support index chaining - you can get to a dictionary from an array via
// a dictionary and so on
// don't worry, the following will not fail with errors in case
// the index path is invalid
if let complicatedAccessOfSomeStringValueOfInterest = info["dictKey"][10]["anotherKey"].string {
// do something
}
else {
// data cannot be indexed
}
// you can also re-use parts of a plist data structure
let info = Plist(...)
let firstSection = info["Sections"][0]["SectionData"]
let sectionKey = firstSection["key"].string!
let sectionSecret = firstSection["secret"].int!
NOTE: To run the example project, clone the repo, and run pod install
from the Example directory first.
iOS 8+, Swift 3.0
Plist is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Plist"
Copy over Plist.swift and place in a project of your choice, including it in your build.
Benzi Ahamed, Twitter
Plist is available under the MIT license. See the LICENSE file for more info.
Contributions encouraged and welcome!