for (Transaction* transaction in transactions) {
//do stuff here, or just print the object with something like the code below
NSLog(@"Transaction: %@", transaction);
}
Конечно, когда у вас есть такой код:
[aTransaction createTransaction:n*100 ofType:credit];
[transactions addObject:aTransaction];
... вы на самом деле не храните вновь созданную транзакцию в массиве. Вы просто храните переменную aTransaction
несколько раз. Возможно, вам повезет больше с чем-то вроде:
Transaction* nextTransaction = [aTransaction createTransaction:n*100 ofType:credit];
[transactions addObject:nextTransaction];
Edit:
У вас может быть больше удачи с:
#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"
int main(int argc, char *argv[]) {
Budget* europeBudget=[[Budget alloc] init];
NSMutableArray *transactions=[[NSMutableArray alloc] initWithCapacity:10];
[europeBudget createBudget:1000.00 withExchangeRate:1.2500];
Transaction* aTransaction = nil;
for(int n=1;n<2;n++){
//this adds 1 transaction to the array
aTransaction = [[[Transaction alloc] init] autorelease];
[aTransaction createTransaction:n*100 ofType:cash];
[transactions addObject:aTransaction];
}
int n=1;
while (n<3) {
//this adds 2 transactions to the array
aTransaction = [[[Transaction alloc] init] autorelease];
[aTransaction createTransaction:n*100 ofType:credit];
[transactions addObject:aTransaction];
n++;
}
do{
//this adds 1 transaction to the array
aTransaction = [[[Transaction alloc] init] autorelease];
[aTransaction createTransaction:n*100 ofType:cash];
[transactions addObject:aTransaction];
n++;
}while (n<=3);
//there should be 4 elements in the array now
NSLog(@"\nNumber of elements in an array:%i",[transactions count]);
int c;
c=[transactions count];
NSLog(@"\nThe Elements are:\n");
for(int i=0;i<c;i++){
Transaction* trans = [transactions objectAtIndex:i];
NSLog(@"Transaction %d: %@; type=%d, amount=%f", i, trans, [trans returnType], [trans returnAmount]);
}
for(Transaction *aaTransaction in transactions){
switch ([aTransaction returnType]) {
case cash:
[europeBudget spendDollars:[aaTransaction returnAmount]];
break;
case credit:
[europeBudget changeForeignCurrency:[aaTransaction returnAmount]];
break;
default:
break;
}
}
[transactions release];
[europeBudget release];
return 0;
}
Адрес, который вы видите в выходных данных, не означает, что программе не удалось найти Transaction
в массиве. На самом деле это означает прямо противоположное. Адрес - это адрес в памяти вашего экземпляра Transaction
. Причина, по которой он печатается как адрес, заключается в том, что при печати экземпляра NSObject
это поведение по умолчанию. Ваш класс не переопределяет это поведение по умолчанию, поэтому вы получаете адрес памяти при его печати.
Если вы хотите переопределить это поведение по умолчанию, вы можете сделать что-то вроде:
#import "Transaction.h"
@implementation Transaction
//override the 'description' method to change how your object prints
-(NSString*)description {
NSString* friendlyType = theType == cash ? @"cash" : @"credit";
return [NSString stringWithFormat:@"Transaction: type=%@, amount=%f", friendlyType, amount];
}
-(void)createTransaction:(double)theAmount ofType:(transactionType)theType{
type=theType;
amount=theAmount;
}
-(double)returnAmount{
return amount;
}
-(transactionType)returnType{
return type;
}
@end