Xcode - Swift - NSURL : "fatal error: unexpectedly found nil while unwrapping an Optional value"
Xcode - Swift - NSURL : "fatal error: unexpectedly found nil while unwrapping an Optional value"
我正在尝试使用 PHP API 和 Swift 客户端在 Xcode 游乐场中测试 OAuth2 实现。基本上,我的代码看起来像这样
let url = NSURL(string: "http://localhost:9142/account/validate")!
var request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody!.setValue("password", forKey: "grant_type")
// Other values set to the HTTPBody
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
// Handle response here
}
但是当我实例化 url 变量时,我一直收到这个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
我尝试在实例化时不展开它,而是在使用它时尝试,它没有改变任何东西,错误出现在我第一次展开它时。
越来越奇怪了..下面
let url = NSURL(string: "http://localhost:9142/account/validate")!
println(url)
产出
http://localhost:9142/account/validate
fatal error: unexpectedly found nil while unwrapping an Optional value
我真的不明白错误是从哪里来的,因为我是 Swift
的新手
发生的事情是您被迫展开设置为 nil 的 HTTPBody,导致此行出现运行时错误:
request.HTTPBody!.setValue("password", forKey: "grant_type")
您需要为请求主体创建一个 NSData 对象,然后按照以下代码将其分配给 request.HTTPBody:
let url = NSURL(string: "http://localhost:9142/account/validate")!
var request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
// Create a parameter dictionary and assign to HTTPBody as NSData
let params = ["grant_type": "password"]
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.allZeros, error: nil)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
// Handle response here
}
我希望这有助于解决您的问题。
更新:
为了在不使用 JSON 序列化程序的情况下序列化数据,您可以创建自己的类似于以下内容的序列化程序:
func dataWithParameterDictionary(dict: Dictionary<String, String>) -> NSData? {
var paramString = String()
for (key, value) in dict {
paramString += "\(key)=\(value)";
}
return paramString.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: false)
}
并这样称呼它:
let dict = ["grant_type": "password"]
let data = dataWithParameterDictionary(dict)
request.HTTPBody = data
我正在尝试使用 PHP API 和 Swift 客户端在 Xcode 游乐场中测试 OAuth2 实现。基本上,我的代码看起来像这样
let url = NSURL(string: "http://localhost:9142/account/validate")!
var request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody!.setValue("password", forKey: "grant_type")
// Other values set to the HTTPBody
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
// Handle response here
}
但是当我实例化 url 变量时,我一直收到这个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
我尝试在实例化时不展开它,而是在使用它时尝试,它没有改变任何东西,错误出现在我第一次展开它时。
越来越奇怪了..下面
let url = NSURL(string: "http://localhost:9142/account/validate")!
println(url)
产出
http://localhost:9142/account/validate fatal error: unexpectedly found nil while unwrapping an Optional value
我真的不明白错误是从哪里来的,因为我是 Swift
的新手发生的事情是您被迫展开设置为 nil 的 HTTPBody,导致此行出现运行时错误:
request.HTTPBody!.setValue("password", forKey: "grant_type")
您需要为请求主体创建一个 NSData 对象,然后按照以下代码将其分配给 request.HTTPBody:
let url = NSURL(string: "http://localhost:9142/account/validate")!
var request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
// Create a parameter dictionary and assign to HTTPBody as NSData
let params = ["grant_type": "password"]
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.allZeros, error: nil)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
// Handle response here
}
我希望这有助于解决您的问题。
更新:
为了在不使用 JSON 序列化程序的情况下序列化数据,您可以创建自己的类似于以下内容的序列化程序:
func dataWithParameterDictionary(dict: Dictionary<String, String>) -> NSData? {
var paramString = String()
for (key, value) in dict {
paramString += "\(key)=\(value)";
}
return paramString.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: false)
}
并这样称呼它:
let dict = ["grant_type": "password"]
let data = dataWithParameterDictionary(dict)
request.HTTPBody = data