copyWithZone(见下文)是否正确,特别是我使用setter来填充新对象的实例变量的位?
@interface Planet : NSObject <NSCopying>
{
NSString *name;
NSString *type;
NSNumber *mass;
int index;
}
@property(copy) NSString *name;
@property(copy) NSString *type;
@property(retain) NSNumber *mass;
@property(assign) int index;
-(void)display;
@end
-(id) copyWithZone: (NSZone *) zone {
Planet *newPlanet = [[Planet allocWithZone:zone] init];
NSLog(@"_copy: %@", [newPlanet self]);
[newPlanet setName:name];
[newPlanet setType:type];
[newPlanet setMass:mass];
[newPlanet setIndex:index];
return(newPlanet);
}
Run Code Online (Sandbox Code Playgroud)
这是一个更好的方法吗?
-(id) copyWithZone: (NSZone *) zone {
Planet *newPlanet = [[[self class] allocWithZone:zone] init];
[newPlanet setName:[self name]];
[newPlanet setType:[self type]];
[newPlanet setMass:[self mass]];
[newPlanet setIndex:[self index]];
return(newPlanet);
}
Run Code Online (Sandbox Code Playgroud)
非常感谢
加里
(对于我们想要制作的副本,假设深层复制是你想要的),使用copyWithZone:用于对象实例变量,并使用=简单地设置原始实例变量.
- (id)copyWithZone:(NSZone *)zone
{
MyClass *copy = [[MyClass alloc] init];
// deep copying object properties
copy.objectPropertyOne = [[self.objectPropertyOne copyWithZone:zone] autorelease];
copy.objectPropertyTwo = [[self.objectPropertyTwo copyWithZone:zone] autorelease];
...
copy.objectPropertyLast = [[self.objectPropertyLast copyWithZone:zone] autorelease];
// deep copying primitive properties
copy.primitivePropertyOne = self.primitivePropertyOne
copy.primitivePropertyTwo = self.primitivePropertyTwo
...
copy.primitivePropertyLast = self.primitivePropertyLast
// deep copying object properties that are of type MyClass
copy.myClassPropertyOne = self.myClassPropertyOne
copy.myClassPropertyTwo = self.myClassPropertyTwo
...
copy.myClassPropertyLast = self.myClassPropertyLast
return copy;
}
Run Code Online (Sandbox Code Playgroud)
但请注意,如果没有copyWithZone,必须设置与self和copy相同的类的属性:否则,这些对象将再次调用此copyWithZone,并将尝试使用copyWithZone设置其myClassProperties.这会触发不必要的无限循环.(另外,你可以调用allocWithZone:而不是alloc:但我很确定alloc:调用allocWithZone:无论如何)
在某些情况下,使用=来深度复制同一个类的对象属性可能不是你想要做的事情,但在所有情况下(据我所知)使用copyWithZone深度复制同一个类的对象属性:或者任何调用copyWithZone:会导致无限循环.
是否是不需要的副本由您决定。将访问器与复制限定符合成的原因是为了确保这些对象的所有权。
但请记住,像 NSNumber 或 NSString 这样的不可变对象在发送 -copy 消息时实际上不会复制它们的存储,它们只会增加它们的保留计数。
| 归档时间: |
|
| 查看次数: |
15297 次 |
| 最近记录: |