我有这个:
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue, {
if let data = NSURLConnection.sendSynchronousRequest(self.buildRequestForVenueLocation(location, identifier), returningResponse: &response, error: &error) {
let responseDictionary: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: .allZeros, error:&error) ?? error?.localizedDescription
dispatch_async(dispatch_get_main_queue(), {
completion(venues: responseDictionary, error: error)
})
} else {
println("There was a problem with the request...")
}
})
我的问题特别是关于这条线:
let responseDictionary: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: .allZeros, error:&error) ?? error?.localizedDescription
这是正确的用法nil coalescing
吗?
根据Apple的文档,这是等效的:
a != nil ? a! : b
如果 a 为 nil,则返回 b,但如果 b 是可选类型,这有关系吗?
编辑
如果有人想知道,这就是我调用该函数时的样子:
v.performVenueLocationRequest(l.location, identifier: "4d4b7105d754a06374d81259") {
if let result = $0 as? [String: AnyObject] ?? $1 {
println(result)
}
}