2

给定一个实现 NSCoding 的类,是否有理由不使用这种模式来实现 copyWithZone: 的实现:

-(instancetype)copyWithZone:(NSZone *)zone{
    return [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self]];
}
4

1 回答 1

1

Just efficiency — the encoding/decoding cost and the total memory footprint.

Suppose you had an object with four immutable instance variables. If you implement a custom copy then you'll allocate one extra instance of that object, then give it ownership of all four instance variables.

If you encode and decode it then there'll be the processing cost of the two-way serialisation and you'll end up with new copies of each of the instance variables.

于 2014-10-15T02:09:47.123 回答