Reputation: 12631
I am new to objective C. I tried to retreive the elements stored in the array but i ended up with some garbage values.I tried in two ways to retreive but could not help me. I have read that in Objective C the objects are stored in the array.Is there any way to retreive the elements from this objects.please help.
Budget* europeBudget=[Budget new];
NSMutableArray *transactions=[[NSMutableArray alloc] initWithCapacity:10];
[europeBudget createBudget:1000.00 withExchangeRate:1.2500];
Transaction* aTransaction;
aTransaction = [Transaction new];
for(NSUInteger n=1;n<2;n++){
[aTransaction createTransaction:n*100 ofType:cash];
[transactions addObject:aTransaction];
}
NSUInteger n=1;
while (n<3) {
[aTransaction createTransaction:n*100 ofType:credit];
[transactions addObject:aTransaction];
n++;
}
do{
[aTransaction createTransaction:n*100 ofType:credit];
[transactions addObject:aTransaction];
n++;
}while (n<=3);
NSLog(@"\nThe Elements are:\n");
int c;
c=[transactions count];
NSLog(@"\nThe Elements are:\n");
for(int i=0;i<c;i++){
NSLog(@"%@",[transactions objectAtIndex:i]);
}
The Elements are:
2011-04-15 10:59:30.515 BudObj.m[569:a0f] <Transaction: 0x10010c900>
2011-04-15 10:59:30.516 BudObj.m[569:a0f] <Transaction: 0x10010c900>
2011-04-15 10:59:30.516 BudObj.m[569:a0f] <Transaction: 0x10010c900>
2011-04-15 10:59:30.517 BudObj.m[569:a0f] <Transaction: 0x10010c900>
Budget* europeBudget=[Budget new];
NSMutableArray *transactions=[[NSMutableArray alloc] initWithCapacity:10];
[europeBudget createBudget:1000.00 withExchangeRate:1.2500];
Transaction* aTransaction;
aTransaction = [Transaction new];
for(NSUInteger n=1;n<2;n++){
[aTransaction createTransaction:n*100 ofType:cash];
[transactions addObject:aTransaction];
}
NSUInteger n=1;
while (n<3) {
[aTransaction createTransaction:n*100 ofType:credit];
[transactions addObject:aTransaction];
n++;
}
do{
[aTransaction createTransaction:n*100 ofType:credit];
[transactions addObject:aTransaction];
n++;
}while (n<=3);
NSLog(@"\nThe Elements are:\n");
for(Transaction* aaTransaction in transactions){
NSLog(@"%@",transactions);
}
The Elements are:
2011-04-15 11:01:30.090 BudObj.m[609:a0f] (
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>"
)
2011-04-15 11:01:30.090 BudObj.m[609:a0f] (
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>"
)
2011-04-15 11:01:30.091 BudObj.m[609:a0f] (
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>"
)
2011-04-15 11:01:30.092 BudObj.m[609:a0f] (
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>",
"<Transaction: 0x10010c900>"
)
Upvotes: 0
Views: 126
Reputation: 4215
You're retrieving the object correct, your issue is with storing it. You're only ever creating one Transaction object. Inside of your loops you're setting the value on the same object on each iteration. So you end up with multiple entries in the array of that one object that has the last value of of each loop stored in it. You need to create the Transaction object inside of the loop and add that new one to the array if you want to store separate values.
Also, just an FYI, your for and do-while loops will only ever evaluate once. The for loop since you're starting with n=1 and only running while N<2. The do-while you don't reset n after the while loop. So it starts off with n=3 and therefore one evaluates once before n>3.
Upvotes: 2
Reputation: 1014
it seems you're not satisfied by default way of string representation of Transaction objects. In this case you should define you own
- (NSString *)description
method which will represent the data of Transaction you want. All other your code seem to be correct
Upvotes: 0