我正在研究我的cloudkit方法的通用“结果”回调,但我不确定为什么无法解释“成功”回调。
参考:https://github.com/apple/swift-evolution/blob/master/proposals/0048-generic-typealias.md
import Foundation
import CloudKit
public enum CloudKitError: Error {
case general(String)
}
public enum CloudKitResult<T, ResultError: Error> {
case success(T)
case failure(ResultError)
}
public typealias CloudKitFetchClosure<T> = (_ result: CloudKitResult<T, CloudKitError>) -> Void
public final class CloudKitController {
public typealias T = CKRecord
public func save(_ record: CKRecord, callback: CloudKitFetchClosure<T>) -> Void {
self.publicDB.save(record, completionHandler: {updatedRecord, error in
if let _: Error = error {
DispatchQueue.main.async(execute: {
callback(.failure(.general("Something went wrong")))
})
} else {
DispatchQueue.main.async(execute: {
callback(.success(updatedRecord))
})
}
})
}
}
member 'success' in 'CloudKitResult<T, CloudKitError>' (aka 'CloudKitResult<CKRecord, CloudKitError>') produces result of type 'CloudKitResult<T, ResultError>', but context expects 'CloudKitResult<T, CloudKitError>' (aka 'CloudKitResult<CKRecord, CloudKitError>')
callback(.success(updatedRecord))
答案 0 :(得分:1)
我必须从CloudKit解包返回的记录。编译器现在很高兴。
public func save(_ record: CKRecord, callback: CloudKitFetchClosure<T>) -> Void {
self.publicDB.save(record, completionHandler: {updatedRecord, error in
if let _: Error = error {
DispatchQueue.main.async(execute: {
callback(.failure(.general("Something went wrong")))
})
} else {
if let ckrecord: CKRecord = updatedRecord {
DispatchQueue.main.async(execute: {
callback(.success(ckrecord))
})
}
}
})
}