如何进行深度复制 Objective-C
How to do deep copying Objective-C
有一个 class 具有以下属性的患者:
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) Symptoms *symptoms;
@property (assign, nonatomic) Status status;
@property (weak, nonatomic) id <PatientDelegate> delegate;
有一个 class 具有属性的症状:
@property (assign, nonatomic) CGFloat temperature;
@property (assign, nonatomic) BOOL headache;
@property (assign, nonatomic) BOOL stomach_ache;
两者 class 都实现了协议 NSCopying:
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
Patient *newPatient = [[[self class] allocWithZone:zone] init];
[newPatient setName:self.name];
[newPatient setSymptoms:self.symptoms];
[newPatient setStatus:self.status];
[newPatient setDelegate:self.delegate];
return newPatient;
}
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
Symptoms *newSymptoms = [[[self class] allocWithZone:zone] init];
[newSymptoms setTemperature:self.temperature];
[newSymptoms setHeadache:self.headache];
[newSymptoms setStomach_ache:self.stomach_ache];
return newSymptoms;
}
还有一个class医生:
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSMutableArray *history;
- (void)makeNoteIn:(Patient *)patient card:(NSMutableArray *)history;
- (void)report;
当病人康复后,医生调用方法 makeNoteIn:
- (void)makeNoteIn:(Patient *)patient card:(NSMutableArray *)history {
Patient *newRecord = [patient copy];
[history addObject:newRecord];
}
记录完成后,所有属性患者return恢复原值。当我们在 makeNoteIn 方法中并且继续处理当前患者时,在历史记录中有 link 到此对象,它具有正确的 属性 值。一旦我们退出该方法或开始处理另一个患者,所有 属性 值都会重置为初始值。
我试过实现复制,还是出错了
当你想要深拷贝一个对象时,你必须在所有子结构上实现copy
:
[newPatient setName:[self.name copy]];
[newPatient setSymptoms:[self.symptoms copy]];
否则它们仍将引用同一个对象,更改一个对象将影响所有对象。
请注意,您可以通过将属性声明为 copy
:
来自动执行此操作
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) Symptoms *symptoms;
通常将 copy
与 NSString
和 NSArray
一起使用,以防止分配可能在外部错误更改的 NSMutableString
和 NSMutableArray
。确保在 Symptoms
.
上实施 NSCopying
有一个 class 具有以下属性的患者:
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) Symptoms *symptoms;
@property (assign, nonatomic) Status status;
@property (weak, nonatomic) id <PatientDelegate> delegate;
有一个 class 具有属性的症状:
@property (assign, nonatomic) CGFloat temperature;
@property (assign, nonatomic) BOOL headache;
@property (assign, nonatomic) BOOL stomach_ache;
两者 class 都实现了协议 NSCopying:
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
Patient *newPatient = [[[self class] allocWithZone:zone] init];
[newPatient setName:self.name];
[newPatient setSymptoms:self.symptoms];
[newPatient setStatus:self.status];
[newPatient setDelegate:self.delegate];
return newPatient;
}
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
Symptoms *newSymptoms = [[[self class] allocWithZone:zone] init];
[newSymptoms setTemperature:self.temperature];
[newSymptoms setHeadache:self.headache];
[newSymptoms setStomach_ache:self.stomach_ache];
return newSymptoms;
}
还有一个class医生:
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSMutableArray *history;
- (void)makeNoteIn:(Patient *)patient card:(NSMutableArray *)history;
- (void)report;
当病人康复后,医生调用方法 makeNoteIn:
- (void)makeNoteIn:(Patient *)patient card:(NSMutableArray *)history {
Patient *newRecord = [patient copy];
[history addObject:newRecord];
}
记录完成后,所有属性患者return恢复原值。当我们在 makeNoteIn 方法中并且继续处理当前患者时,在历史记录中有 link 到此对象,它具有正确的 属性 值。一旦我们退出该方法或开始处理另一个患者,所有 属性 值都会重置为初始值。
我试过实现复制,还是出错了
当你想要深拷贝一个对象时,你必须在所有子结构上实现copy
:
[newPatient setName:[self.name copy]];
[newPatient setSymptoms:[self.symptoms copy]];
否则它们仍将引用同一个对象,更改一个对象将影响所有对象。
请注意,您可以通过将属性声明为 copy
:
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) Symptoms *symptoms;
通常将 copy
与 NSString
和 NSArray
一起使用,以防止分配可能在外部错误更改的 NSMutableString
和 NSMutableArray
。确保在 Symptoms
.
NSCopying