Swift 将 Plist(在 NSString 内)转换为 NSDictionary
Swift convert Plist(inside NSString) to NSDictionary
你好,我有一个包含 Plist 数据的 NSString。我需要将它转换为 NSDictionary。我找到了用文件来做的方法,但我想直接在内存中做。我怎样才能用 swift 做到这一点?谢谢你的帮助。
var plist : NSMutableDictionary
let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {
if paths.count > 0 {
if let dirPath = paths[0] as? String {
let readPath = dirPath.stringByAppendingPathComponent("info.plist")
plist = NSMutableDictionary(contentsOfFile: readPath)!
println(plist)
}
}
}
经过大量的咆哮和玩耍,我发现了它
var data = stringWithPlistInside.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
var error : NSError?
var dic: AnyObject! = NSPropertyListSerialization.propertyListWithData(data!, options: 0, format: nil, error: &error)
以防其他人碰巧遇到我的情况。
你好,我有一个包含 Plist 数据的 NSString。我需要将它转换为 NSDictionary。我找到了用文件来做的方法,但我想直接在内存中做。我怎样才能用 swift 做到这一点?谢谢你的帮助。
var plist : NSMutableDictionary
let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {
if paths.count > 0 {
if let dirPath = paths[0] as? String {
let readPath = dirPath.stringByAppendingPathComponent("info.plist")
plist = NSMutableDictionary(contentsOfFile: readPath)!
println(plist)
}
}
}
经过大量的咆哮和玩耍,我发现了它
var data = stringWithPlistInside.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
var error : NSError?
var dic: AnyObject! = NSPropertyListSerialization.propertyListWithData(data!, options: 0, format: nil, error: &error)
以防其他人碰巧遇到我的情况。