请看下面的代码:- .h
@interface BankAccount : NSObject<NSCopying>
{
double accountBalance;
long accountNumber;
NSString *CustomerName;
NSString *AccountType;
}
-(void) setAccount: (long) y andBalance: (double) x;
-(void) setCustomerName: (NSString*) name andAccountType: (NSString*) type;
-(id)copyWithZone:(NSZone *)zone;
@end
@interface Savings : BankAccount
{
int number;
NSString *Offer;
}
-(void) setSavingNumber: (uint8_t) num andOffer: (NSString*) offer;
-(id)copyWithZone:(NSZone *)zone;
@end- .m
@implementation BankAccount
-(void) setAccount: (long) y andBalance: (double) x
{
accountNumber = y;
accountBalance = x;
}
-(void) setCustomerName: (NSString*) name andAccountType: (NSString*) type
{
CustomerName = name;
AccountType = type;
}
-(id)copyWithZone:(NSZone *)zone
{
BankAccount *accountCopy = [[BankAccount allocWithZone: zone] init];
[accountCopy setAccount: accountNumber andBalance: accountBalance];
[accountCopy setCustomerName:CustomerName andAccountType:AccountType];
return accountCopy;
}
@end
@implementation Savings
-(void) setSavingNumber: (uint8_t) num andOffer: (NSString*) offer
{
number = num;
Offer = offer;
}
-(id)copyWithZone:(NSZone *)zone
{
Savings * clone = [super copyWithZone:zone];
[clone setSavingNumber:number andOffer:Offer];************** error *********
return clone;
}
@end运行此代码时:
Savings* account1;
Savings* account2;
account1 = [[Savings alloc] init];
[account1 setAccount:10 andBalance:1000.10];
[account1 setCustomerName:[NSString stringWithFormat:@"%@",@"Deepak"] andAccountType:[NSString stringWithFormat:@"%@",@"Savings"]];
[account1 setSavingNumber:2001 andOffer:@"Bad"];
account2 = [account1 copy];我不知道代码有什么问题,请帮帮我。提前谢谢。
谢谢Deepak
发布于 2011-05-27 02:52:31
您的基本copyWithZone:方法应如下所示:
-(id)copyWithZone:(NSZone *)zone {
// change this line to use [self class]
BaseClass *base = [[[self class] allocWithZone:zone] init];
// copy base members:
// ...
return base;
}派生的copyWithZone:方法应该如下所示:
-(id)copyWithZone:(NSZone *)zone {
DerivedClass *derived = [super copyWithZone:zone];
// copy derived members:
// ...
return derived;
}还要确保你正在制作强引用的深层副本和弱引用的浅副本。因此,例如,要复制类型为NSString和NSArray的成员(每个成员都具有强引用成员,一个成员具有弱成员),请执行以下操作:
derived.strongString = [[strongString copyWithZone:zone] autorelease];
derived.weakString = weakString;
derived.strArrWStrVals = [[strArrWStrVals copyWithZone:zone] autorelease];
derived.strArrWWeakVals = [[[NSArray allocWithZone:zone]
initWithArray:strArrWWeakVals] autorelease];
derived.weakArray = weakArray;(通常还会分配/保留弱成员,并复制强变量。)
请注意,由于这个原因,您不应该使用initWithMyself:类型方法来复制数据。
发布于 2010-08-07 11:20:02
好的,首先,您的测试代码是错误的,因为您在一个8位整数中设置了保存数字2001。其次,您的代码不会运行,因为您试图在BankAccount而不是Saving对象上调用setSavingNumber:andOffer:,这样它就无法在运行时找到此方法的选择器。感谢David指出这一点。
在实现BankAccount::copyWithZone时,您使用了alloc-init,并且返回的对象没有问题。在实现Savings::copyWithZone时,调用超级copyWithZone。你得到的是一个BankAccount类型的对象,所以你不能对它使用setSavingNumber:和of:。由于您在基类中使用了alloc-init,因此还应该在Savings类中使用alloc-init和setMethods。
为了避免重复代码,我会重新注释在BankAccount中实现一个initWithBankAccount,在Savings中也是如此。
然后在copyWithZone中,你会有
return [[BankAccount allocWithZone:zone] initWithBankAccount:self];和
return [[Savings allocWithZone:zone] initWithSavings:self];尽管您必须确保在initWithSavings中调用
self = [super initWithBankAccount:savings];或者直接调用init并复制基成员初始化。
看看内存管理编程指南中的Implementing object copy。
https://stackoverflow.com/questions/3423366
复制相似问题