NSCopying & Inheritance

Please see the following code: -------. 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

When running this code ::

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];
#

I do not know what is wrong with the code, please help me. thanks in advance.

Thanks Deepak

+5
source share
2 answers

Your base method copyWithZone:should look like this:

-(id)copyWithZone:(NSZone *)zone {
    // change this line to use [self class]
    BaseClass *base = [[[self class] allocWithZone:zone] init];

    // copy base members:
    // ...

    return base;
}

Your derived methods copyWithZone:should look like this:

-(id)copyWithZone:(NSZone *)zone {
    DerivedClass *derived = [super copyWithZone:zone];

    // copy derived members:
    // ...

    return derived;
}

Also make sure you make deep copies of strong links and shallow copies of weak links. Thus, for example, to copy members of type NSStringand NSArray(each of them with strongly referenced elements and one with weak):

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;

(Usually weak members are also assigned / saved, and strong variables are copied.)

, - initWithMyself: .

+28

, , 2001 8- . -, , setSavingNumber: andOffer: BankAccount Saving, . .

BankAccount:: copyWithZone alloc-init, , . Savings:: copyWithZone super copyWithZone. , , BankAccount, setSavingNumber: andOffer: . alloc-init , alloc-init setMethods Savings.

, initWithBankAccount BankAccount Savings.

copyWithZone

return [[BankAccount allocWithZone:zone] initWithBankAccount:self];

return [[Savings allocWithZone:zone] initWithSavings:self];

, initWithSavings

self = [super initWithBankAccount:savings];

init, .

.

+1
source

All Articles