一个简单的Swift库,用于处理plist文件和数据。受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!
注意:要运行示例项目,请克隆仓库,然后首先在Example目录中运行pod install
。
iOS 8+
Plist可以通过CocoaPods获取。要安装它,只需将以下行添加到Podfile中:
pod "Plist"
复制Plist.swift并将其放置在您选择的项目的适当位置,并将其包含在构建中。
Benzi Ahamed,Twitter
Plist可在MIT许可证下获得。有关更多信息,请参阅LICENSE文件。
鼓励并欢迎贡献!