在 Swift 3 中, closures 預設為 @noescape 的形式, 並將 Swift 2中的 noescape 關鍵字 deprecated。
// Swift 3 class HealthKitManager: NSObject { private let healthStore = HKHealthStore() func requestAuthorization(completion: @escaping (Bool, Error?) -> Void) { var shareTypes = Set<HKSampleType>() var readTypes = Set<HKSampleType>() // Add Workout Type shareTypes.insert(HKSampleType.workoutType()) readTypes.insert(HKSampleType.workoutType()) // Request Authorization healthStore.requestAuthorization(toShare: shareTypes, read: readTypes, completion: completion) } }
Escaping Closures
A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write
@escaping
before the parameter’s type to indicate that the closure is allowed to escape.
探索更多來自 懶泥陳的閱讀書房 的內容
訂閱後即可透過電子郵件收到最新文章。