首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CloudKit CKError扩展在Objective-C中不可用?

CloudKit CKError扩展在Objective-C中不可用?
EN

Stack Overflow用户
提问于 2019-09-09 16:45:51
回答 1查看 150关注 0票数 0

我在这里读到CKError在Objective-C中不可用,我同意这一点。例如,此扩展在Swift中可用。

代码语言:javascript
复制
@available(OSX 10.10, iOS 8.0, watchOS 3.0, *)
extension CKError {

    /// Retrieve partial error results associated by item ID.
    public var partialErrorsByItemID: [AnyHashable : Error]? { get }

    /// The original CKRecord object that you used as the basis for
    /// making your changes.
    public var ancestorRecord: CKRecord? { get }

    /// The CKRecord object that was found on the server. Use this
    /// record as the basis for merging your changes.
    public var serverRecord: CKRecord? { get }

    /// The CKRecord object that you tried to save. This record is based
    /// on the record in the CKRecordChangedErrorAncestorRecordKey key
    /// but contains the additional changes you made.
    public var clientRecord: CKRecord? { get }

    /// The number of seconds after which you may retry a request. This
    /// key may be included in an error of type
    /// `CKErrorServiceUnavailable` or `CKErrorRequestRateLimited`.
    public var retryAfterSeconds: Double? { get }
}

问题是我在Objective-C项目中需要这些对象。我以某种方式(我相信)设法在Objective-C中获得了partialErrorsByItemID,方法是为NSError创建一个类别,并稍微理解一下CKError.h的文档,如下所示:

代码语言:javascript
复制
CKErrorCode ckErrorCode = (CKErrorCode) _code;
if (ckErrorCode == CKErrorPartialFailure) {
    // When a CKErrorPartialFailure happens this key will be set in the error's userInfo dictionary.
    // The value of this key will be a dictionary, and the values will be errors for individual items with the keys being the item IDs that failed.

    NSDictionary *dicError = _userInfo;

    if ([dicError objectForKey:CKPartialErrorsByItemIDKey] != nil) {

        NSDictionary *dic = (NSDictionary *)[dicError objectForKey:CKPartialErrorsByItemIDKey];

        for (NSString* key in dic) {
            NSError *newError = dic[key];
            if (code == newError.code) {
                match = YES;
            }
        }

    } else {
        return NO;
    }
}

但同样,我的问题是如何获取对象serverRecordclientRecord。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-09 23:33:49

这里有一个Objective-C类别,它复制了Swift的大部分CKError结构。我没有添加errorCodelocalizedDescriptionerrorUserInfo,因为NSError已经提供了codelocalizedDescriptionuserInfo

CloudKitExtensions.h

代码语言:javascript
复制
#import <CloudKit/CloudKit.h>

NS_ASSUME_NONNULL_BEGIN

extern const double UnknownRetrySeconds;

@interface NSError (CKError)

- (NSDictionary<id, NSError *> * _Nullable)partialErrorsByItemID;
- (CKRecord * _Nullable)ancestorRecord;
- (CKRecord * _Nullable)clientRecord;
- (CKRecord * _Nullable)serverRecord;
- (double)retryAfterSeconds; // returns UnknownRetrySeconds if not available

@end

NS_ASSUME_NONNULL_END

CloudKitExtensions.m

代码语言:javascript
复制
#import "CloudKitExtensions.h"

const double UnknownRetrySeconds = -1;

@implementation NSError (CKError)

- (NSDictionary<id, NSError *> * _Nullable)partialErrorsByItemID {
    if ([self.domain isEqualToString:CKErrorDomain] && self.code == CKErrorPartialFailure) {
        return self.userInfo[CKPartialErrorsByItemIDKey];
    } else {
        return nil;
    }
}

- (CKRecord * _Nullable)ancestorRecord {
    if ([self.domain isEqualToString:CKErrorDomain] && self.code == CKErrorServerRecordChanged) {
        return self.userInfo[CKRecordChangedErrorAncestorRecordKey];
    } else {
        return nil;
    }
}

- (CKRecord * _Nullable)clientRecord {
    if ([self.domain isEqualToString:CKErrorDomain] && self.code == CKErrorServerRecordChanged) {
        return self.userInfo[CKRecordChangedErrorClientRecordKey];
    } else {
        return nil;
    }
}

- (CKRecord * _Nullable)serverRecord {
    if ([self.domain isEqualToString:CKErrorDomain] && self.code == CKErrorServerRecordChanged) {
        return self.userInfo[CKRecordChangedErrorServerRecordKey];
    } else {
        return nil;
    }
}

- (double)retryAfterSeconds {
    if ([self.domain isEqualToString:CKErrorDomain]) {
        NSNumber *delayVal = self.userInfo[CKErrorRetryAfterKey];
        return delayVal ? [delayVal doubleValue] : UnknownRetrySeconds;
    } else {
        return UnknownRetrySeconds;
    }
}

@end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57850572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档