gpt4 book ai didi

ios - 将字符串转换为 plist - swift

转载 作者:行者123 更新时间:2023-11-28 10:25:12 26 4
gpt4 key购买 nike

看来我可能是唯一一个尝试这样做的人,或者说这很简单,除了我之外每个人都已经知道怎么做了!

网站请求返回一个 plist 响应,响应以字符串形式接收。然后如何将该 plist“字符串”放入字典中?

let url = NSURL(string: "http://...")
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
request.HTTPMethod = "POST"

let boundaryConstant = "----------V2ymHFg03esomerasdfsalfkjlksjdfy"
let contentType = "mulutipart/form-data; boundary=" + boundaryConstant
NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)
var dataString = ""
let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = requestBodyData

var response: NSURLResponse? = nil
var error: NSError? = nil
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)

let results = NSString(data: reply!, encoding: NSUTF8StringEncoding)

results 是一个 plist 字符串,带有 xml 头和 plist 结构后的数据。如果我使用

,plist 返回效果很好
NSDictionary(contentsOfURL: NSURL(string: path)!)!

但是,我是在发布,而不是“获取”,所以据我所知我不能使用 NSURL 路径。

提前致谢!

最佳答案

NSPropertyListSerialization 是你的 friend ,只要你真正得到 plist 数据:

if let plistData = results.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) {
var format = NSPropertyListFormat.XMLFormat_v1_0
let options = NSPropertyListMutabilityOptions.Immutable
if let dict = NSPropertyListSerialization.propertyListWithData(plistData,
options: NSPropertyListReadOptions(options.rawValue),
format: &format,
error: nil) as? NSDictionary {
println(dict)
}
}

关于ios - 将字符串转换为 plist - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27800447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com