Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your ultimate problem is you are using the same transaction object. You set aTransaction once on this line and never set it anywhere else. I do see from your comments it was once right though.</p> <pre><code>Transaction* aTransaction; aTransaction = [Transaction new]; </code></pre> <p>When you add the the transaction to the array all of them point to the same object and what ever change you make to what you are thinking is 1 aTransaction is really changing them all. So when it comes time to print you have multiple references to the same transaction all of type credit and you keep sending the same return amount.</p> <p>Also remember that indexes are usually 0 based and <code>int n = 1;</code> may not get you the correct number elements you are looking for.</p> <p>To achieve your expected output with as close to your orignal code as I can try this. (may need some tweaking)</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]; [europeBudget createBudget:1000.00 withExchangeRate:1.2500]; Transaction* aTransaction; for(int n=1;n&lt;2;n++){ aTransaction = [[Transaction alloc] init]; [aTransaction createTransaction:n*100 ofType:cash]; [transactions addObject:aTransaction]; [aTransaction release]; } int n=0; while (n&lt;3) { aTransaction = [[Transaction alloc] init]; [aTransaction createTransaction:n*100 ofType:credit]; [transactions addObject:aTransaction]; [aTransaction release]; n++; } //n = 3 at this point do{ aTransaction = [[Transaction alloc] init]; [aTransaction createTransaction:n*100 ofType:cash]; [transactions addObject:aTransaction]; [aTransaction release]; n++; }while (n&lt;=3); //n = 4 at this point aTransaction = [[Transaction alloc] init]; [aTransaction createTransaction:n*100 ofType:credit]; [transactions addObject:aTransaction]; [aTransaction release]; 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>Now I did not check that your dollar amounts are correct but this should print out the correct order of Converting/Charging. I also didn't assume there was Garbage Collection so I just added all the releases.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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