Is returning [self retain] in copyWithZone for immutable classes with mutable subclasses really safe / a good idea?

一个人想着一个人 提交于 2019-12-05 11:20:19

Your best option would be to have an initWithMyImmutableObject initialiser in your immutable superclass. Your subclass can then just implement NSCopying with

- (id) copyWithZone:(NSZone*)zone {
    return [[[self superclass] alloc] initWithMyImmutableObject:self]
}

That way the actual copying of properties is done in a method of your superclass, which has access to all private members that need to be copied.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!