Situation:
- Set two instance variable
- Display variables in the console
(I know that I can use properties instead
of coding the setter and getter myself but
I like to understand why my code is not working.
The output on the console I get is:
2012-01-12 12:04:23.099 Test212[5894:707] Number: 0.000000 – Balance:
0.000000.
Why I don’t get the values I set? Number = 1234 & Balance = 500?
)
BankAccount.h
#import <Foundation/Foundation.h>
@interface BankAccount : NSObject
{
double bankAccountBalance;
double bankAccountNumber;
}
-(void) setBankAccountBalance: (double)b;
-(void) setBankAccountNumber: (double)n;
-(double)showBankAccountBalance;
-(double) showBankAccountNumber;
@end
BankAccount.m
#import "BankAccount.h"
@implementation BankAccount
-(void) setBankAccountBalance: (double)b
{
b = bankAccountBalance;
}
-(void) setBankAccountNumber: (double)n
{
n = bankAccountNumber;
}
-(double)showBankAccountBalance
{
return bankAccountBalance;
}
-(double) showBankAccountNumber
{
return bankAccountNumber;
}
@end
main.m
#import <Foundation/Foundation.h>
#import "BankAccount.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
BankAccount *account1 = [[BankAccount alloc] init];
[account1 setBankAccountNumber:1234];
[account1 setBankAccountBalance:500];
NSLog(@"Number: %f - Balance: %f.",[account1 showBankAccountNumber], [account1 showBankAccountBalance]);
}
return 0;
}
You do the wrong assignments in the setter methods. They should be
and