【发布时间】:2015-03-09 00:57:34
【问题描述】:
我想使用 NSURL 会话将 deviceToken 发送到我的服务器,但每次都会崩溃。我试图找到一种将 DataObject(deviceToken)转换为 NSString 的方法,但到目前为止没有成功。
错误:“致命错误:在展开可选值时意外发现 nil”
非常感谢任何帮助。这是我的代码
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!) {
let urlPath = "http://example.com/deviceToken=\(deviceToken)"
let url = NSURL(string: urlPath)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
if(error != nil) {
// If there is an error in the web request, print it to the console
println(error.localizedDescription)
}
var err: NSError?
})
task.resume()
}
【问题讨论】:
-
"[http://example.com]&deviceToken=\(deviceToken)"应该是"http://example.com&deviceToken=\(deviceToken)",不带方括号。你从哪里得到的? -
其实应该是
"http://example.com?deviceToken=\(deviceToken)"。