copyWithZone :(深拷贝)在子类中崩溃

我尝试创建一个赋予NSCopying协议的复制方法。 我有以下课程:
@interface Gene : NSObject <NSCopying>
{

    int firstAllele;
    int secondAllele;

}
用方法:
-(id) copyWithZone:(NSZone*) zone
{
    id clonedGene = [[[self class] allocWithZone:zone] initWithAllele1:first andAllele2:second];

    return clonedGene;
}
如果我按以下方式调用该方法:
Gene* gene1 = [[Gene alloc]initWithAllele1:4 andAllele2:2];
Gene* gene2 = [gene1 copy];
它在调用gene1的复制方法时崩溃了。 我必须以不同方式调用该方法吗? 像
[gene1 copyWithZone:(NSZone *)]
但我必须通过什么对象?我是否必须创建一个NSZone对象?还是有一个我可以作为参数传递的默认值? 感谢您的任何帮助     
已邀请:
我弄清楚了: 我将Gene类更改为:
@interface Gene : NSObject 
{
    Allele * first;
    Allele * second;   
}
我还需要创建我添加的对象的副本,以及确认复制协议所需的子对象:
-(id) copyWithZone:(NSZone*) zone  
{  
    id clonedGene = [[[self class] allocWithZone:zone] initWithAllele1:[first copy] andAllele2:[second copy]];   
    return clonedGene;  
}
所以我不得不定义一个
-(id) copyWithZone:(NSZone*) zone;
Allele类中的方法:
-(id) copyWithZone:(NSZone*) zone  
{  
    id copiedAllele = [[[self class] allocWithZone:zone] initWithAllele:allele];    
    return copiedAllele;  
}
并且因为allele是枚举类型,所以不需要实现任何更深层次的复制方法(因为它是基本类型)。 因此,如果我想实现深度复制方法,我必须确保用作属性的所有类都具有实现的复制功能。 谢谢你的帮助,我希望我能回答我自己的问题。 亲切的问候     

要回复问题请先登录注册