Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>for (Transaction* transaction in transactions) { //do stuff here, or just print the object with something like the code below NSLog(@"Transaction: %@", transaction); } </code></pre> <p>Of course, when you have code like this:</p> <pre><code>[aTransaction createTransaction:n*100 ofType:credit]; [transactions addObject:aTransaction]; </code></pre> <p>...you are not actually storing the newly created transaction in the array. You are just storing the variable <code>aTransaction</code> multiple times. You may have better luck with something like:</p> <pre><code>Transaction* nextTransaction = [aTransaction createTransaction:n*100 ofType:credit]; [transactions addObject:nextTransaction]; </code></pre> <p><strong>Edit:</strong></p> <p>You may have more luck with:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #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&lt;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&lt;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&lt;=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&lt;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; } </code></pre> <p>The address that you see in your output doesn't mean that the program couldn't find the <code>Transaction</code> in the array. In fact, it means the exact opposite. The address is the address in memory of your <code>Transaction</code> instance. The reason it gets printed as an address is because that is the default behavior when printing an <code>NSObject</code> instance. Your class is not overriding this default behavior, so you get the memory address when you print it. </p> <p>If you want to override this default behavior, you can do something like:</p> <pre><code>#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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload