Jonas Aqua Jonas Aqua - 3 months ago 85
iOS Question

Swift 3.0 CloudKit fetchDatabaseChangesCompletionBlock Error

I am following along a WWDC Session about CloudKit. In this, there was this code snipped:

let changesOperation = CKFetchDatabaseChangesOperation(previousServerChangeToken: privateDatabaseChangeToken)
//(...)
changesOperation.fetchDatabaseChangesCompletionBlock = {
(newToken: CKServerChangeToken?, more: Bool, error: NSError?) -> Void in
// error handling here
self.sharedDBChangeToken = newToken // cache new token
self.fetchZoneChanges(callback) // using CKFetchRecordZoneChangesOperation
}


Even though the code was exactly copied from the slides, XCode spilled out this error: " Cannot assign value of type '(CKServerChangeToken?, Bool, NSError?) -> Void' to type '((CKServerChangeToken?, Bool, Error?) -> Void)?' ". I am using XCode 8.0 Beta 4 and the target is iOS 10, in case this should be a compiler error.

Answer

In Beta 4[citation needed], the swift-evolution proposal SE-0112 was implemented, affecting error handling.

The important bit here is that Objective-C NSError types are imported in Swift as Error (formerly ErrorProtocol) types instead of NSError.

Just update your type annotation:

changesOperation.fetchDatabaseChangesCompletionBlock = {
    (newToken: CKServerChangeToken?, more: Bool, error: Error?) -> Void in
    // ...
}

As a side note, you can actually omit the parameter types entirely:

changesOperation.fetchDatabaseChangesCompletionBlock = { newToken, more, error in
    // ...
}

For error handling, from what I can tell the error passed is often a CKError, which is a new(?) struct which encapsulates all(?) of the userInfo dictionary on the old NSError.

changesOperation.fetchDatabaseChangesCompletionBlock = { newToken, more, error in
    guard error == nil else {
        if let ckerror = error as? CKError {
            // ... Handle the CKError
        }
        return
    }
    // ... Do something useful
}
Comments