Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to retrieve and view the elements from the NSMutableArray
    text
    copied!<p>I am trying some examples from the book "Objective C for Dummies". I tried to retrieve the elements with this code below but in vain. All are considered as Objects in the NSMutableArray. But I don't know how to retrieve the elements using the objects.</p> <h2>main.m</h2> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "BudObj.h" #import "Transaction.h" int main(int argc, char *argv[]) { Budget* europeBudget=[Budget new]; NSMutableArray *transactions=[[NSMutableArray alloc] initWithCapacity:10]; [europeBudget createBudget:1000.00 withExchangeRate:1.2500]; Transaction* aTransaction; aTransaction = [Transaction new]; 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=1; while (n&lt;3) { aTransaction = [[Transaction alloc]init]; [aTransaction createTransaction:n*100 ofType:credit]; [transactions addObject:aTransaction]; [aTransaction release]; n++; } do{ aTransaction = [[Transaction alloc]init]; [aTransaction createTransaction:n*100 ofType:cash]; [transactions addObject:aTransaction]; [aTransaction release]; n++; }while (n&lt;=3); 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++){ NSLog(@"%@",[transactions objectAtIndex:i]); } 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> <h2>BudObj.h</h2> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Budget : NSObject { float exchangeRate; double budget; double exchangeTransaction; } - (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate; - (void) spendDollars: (double) dollars; - (void) changeForeignCurrency: (double) foreignCurrency; @end </code></pre> <h2>BudObj.m</h2> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "BudObj.h" #import "Transaction.h" @implementation Budget - (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate{ budget = aBudget; exchangeRate = anExchangeRate; } - (void) spendDollars:(double)dollars{ budget = budget - dollars; NSLog(@"Converting %0.2f US Dollars into Foreign Currency leaves $%0.2f",dollars,budget); } - (void) changeForeignCurrency:(double)foreignCurrency{ exchangeTransaction = foreignCurrency * exchangeRate; budget = budget - exchangeTransaction; NSLog(@"Charging %0.2f in Foreign Currency leaves $%0.2f",foreignCurrency,budget); } @end </code></pre> <h2>Transaction.h</h2> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; typedef enum{cash,credit} transactionType; @interface Transaction : NSObject { transactionType type; double amount; } -(void)createTransaction:(double)theAmount ofType:(transactionType)theType; -(double)returnAmount; -(transactionType)returnType; @end </code></pre> <h2>Transaction.m</h2> <pre><code>#import "Transaction.h" @implementation Transaction -(void)createTransaction:(double)theAmount ofType:(transactionType)theType{ type=theType; amount=theAmount; } -(double)returnAmount{ return amount; } -(transactionType)returnType{ return type; } @end </code></pre> <h2>Output</h2> <pre><code>The Elements are: 2011-04-15 18:12:11.039 BudObj.m[2180:a0f] &lt;Transaction: 0x10010c950&gt; //Could not retreive the data from the array it's showing up some address 2011-04-15 18:12:11.039 BudObj.m[2180:a0f] &lt;Transaction: 0x100104fe0&gt; // 2011-04-15 18:12:11.040 BudObj.m[2180:a0f] &lt;Transaction: 0x100106c60&gt; // 2011-04-15 18:12:11.040 BudObj.m[2180:a0f] &lt;Transaction: 0x100106d00&gt; // 2011-04-15 18:12:11.041 BudObj.m[2180:a0f] Converting 100.00 US Dollars into Foreign Currency leaves $900.00 2011-04-15 18:12:11.041 BudObj.m[2180:a0f] Converting 100.00 US Dollars into Foreign Currency leaves $800.00 2011-04-15 18:12:11.041 BudObj.m[2180:a0f] Converting 200.00 US Dollars into Foreign Currency leaves $600.00 2011-04-15 18:12:11.042 BudObj.m[2180:a0f] Converting 300.00 US Dollars into Foreign Currency leaves $300.00 </code></pre>
 

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