首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSCopying协议不工作

NSCopying协议不工作
EN

Stack Overflow用户
提问于 2016-05-13 02:26:39
回答 1查看 423关注 0票数 0

我正在试图复制一个对象,并且实现了NSCopying协议,如下所示:

代码语言:javascript
复制
#MyActivity.h
@interface MyActivity : MyModel <NSCopying>
{
    NSInteger activityId;
    NSInteger userId;
    NSInteger checkinId;
    NSString *status;
    NSString *dateCreated;
    NSString *dateModified;
    NSString *dateStart;
    NSString *dateEnd;
    NSString *activityDescription;
    NSString *name;
    NSString *type;
    NSString *repeat;
    NSString *routineName;
    NSString *startTimezone;
    NSString *endTimezone;
    GUILocation *location;
}

@property NSInteger activityId;
@property NSInteger userId;
@property NSInteger checkinId;

@property (nonatomic, strong) NSString *status;
@property (nonatomic, strong) NSString *dateCreated;
@property (nonatomic, strong) NSString *dateModified;
@property (nonatomic, strong) NSString *dateStart;
@property (nonatomic, strong) NSString *dateEnd;
@property (nonatomic, strong) NSString *activityDescription;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSString *repeat;
@property (nonatomic, strong) NSString *routineName;
@property (nonatomic, strong) NSString *startTimezone;
@property (nonatomic, strong) NSString *endTimezone;

@property (nonatomic, strong) MyLocation *location;

-(id)copyWithZone:(NSZone *)zone;
...

这就是我的实现文件的样子:

代码语言:javascript
复制
#MyActivity.m
...
-(id)copyWithZone:(NSZone *) zone
{
    GUIActivity* copyActivity        = [super copyWithZone:zone];
    copyActivity.activityId          = self.activityId;
    copyActivity.userId              = self.userId;
    copyActivity.checkinId           = self.checkinId;
    copyActivity.status              = self.status;
    copyActivity.dateCreated         = self.dateCreated;
    copyActivity.dateModified        = self.dateModified;
    copyActivity.dateStart           = self.dateStart;
    copyActivity.dateEnd             = self.dateEnd;
    copyActivity.activityDescription = self.activityDescription;
    copyActivity.name                = self.name;
    copyActivity.type                = self.type;
    copyActivity.repeat              = self.repeat;
    copyActivity.routineName         = self.routineName;
    copyActivity.startTimezone       = self.startTimezone;
    copyActivity.endTimezone         = self.endTimezone;
    copyActivity.location            = [self.location copyWithZone:zone];


    return copyActivity;
}
...

当我试图通过实现此方法进行复制时:

代码语言:javascript
复制
- (void)addActivity:(MyActivity *)activity
          toResults:(NSMutableArray *)results
           withDate:(NSDate *)date
{
    MyActivity *actNow     = [activity copy];
    actNow.dateStart       = [NSDate stringFromDate:date];
    [results addObject:actNow];
}

我仍然会发现错误:

*由于“NSInvalidArgumentException”异常终止应用程序,原因:'-RLMAccessor_v0_MyActivity copyWithZone::未识别的选择器发送到实例0x7fe5e0c2c0a0‘

MyActivityRLMObject的子类,所以我不确定这是否与问题有关。有人能给我指明正确的方向吗?

EN

回答 1

Stack Overflow用户

发布于 2016-05-13 05:24:24

领域内部覆盖它管理的属性的访问器,因此不可能使它符合传统意义上的NSCopying

如果要在RLMObject上执行完整的深度复制,则Realm-JSON库实际上提供了做这件事的方法

代码语言:javascript
复制
- (instancetype)deepCopy {
    RLMObject *object = [[NSClassFromString(self.objectSchema.className) alloc] init];

    for (RLMProperty *property in self.objectSchema.properties) {

        if (property.type == RLMPropertyTypeArray) {
            RLMArray *thisArray = [self valueForKeyPath:property.name];
            RLMArray *newArray = [object valueForKeyPath:property.name];

            for (RLMObject *currentObject in thisArray) {
                [newArray addObject:[currentObject deepCopy]];
            }

        }
        else if (property.type == RLMPropertyTypeObject) {
            RLMObject *value = [self valueForKeyPath:property.name];
            [object setValue:[value deepCopy] forKeyPath:property.name];
        }
        else {
            id value = [self valueForKeyPath:property.name];
            [object setValue:value forKeyPath:property.name];
        }
    }

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

https://stackoverflow.com/questions/37200300

复制
相关文章

相似问题

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