Я новичок в задаче C. Я пытался получить элементы, хранящиеся в массиве, но у меня были некоторые значения мусора. Я пытался получить два пути, но не смог мне помочь.
Я читал, что в Objective C объекты хранятся в массиве. Есть ли способ извлечь элементы из этих объектов. Пожалуйста, помогите.
Метод 1:
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>
Метод 2:
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>"
)