私はこれを持っています:
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)
}
}