默认值我有这样的:无凝聚:使用可选的类型,在斯威夫特
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
这个正确使用?
根据苹果的文档,这是equvivalent:
a != nil ? a! : b
如果是零,则返回B,但它是物质如果B是一个可选的类型?
编辑
为了防止有人知道,这里是什么样子时,我调用该函数,如:
v.performVenueLocationRequest(l.location, identifier: "4d4b7105d754a06374d81259") {
if let result = $0 as? [String: AnyObject] ?? $1 {
println(result)
}
}
明白了,谢谢!在我的情况,但 - 传递给接受两个可选参数反正封字典,然后我在实际的函数调用 –