wood burning stoves 2.0*
The moose likes iOS and the fly likes Accesing a method for a object in an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Mobile » iOS
Bookmark "Accesing a method for a object in an array" Watch "Accesing a method for a object in an array" New topic
Author

Accesing a method for a object in an array

Anders hofel
Greenhorn

Joined: Apr 15, 2013
Posts: 9
Hi fellas,

All new to objective c, and i have some problems with the following simple code.


int i = 0;
for(Item *q in array){
valueInDollars += [array objectAtIndex:i "getValueInDollars"];
i++;
}

The "getValueInDollars" is how i want it to work - i obviously have a getValueInDollars method in my Item class, but i don't know the correct syntactic way to access it.

In java it would be something like array[i].getValueInDollars....

i hope its clear what I'm trying to achieve.
Bernhard Haeussermann
Ranch Hand

Joined: Aug 05, 2009
Posts: 38
Hi Anders,

the syntax for calling a method having no parameters on an object in Objective-C is as follows:

The syntax for calling a method with a number of parameters is:

In this example the name of the method is myMethodParam1:param2:param3: (each ':' acts as a placeholder for a parameter) and paramValue1, paramValue2, and paramValue3 are names of variables.

In Objective-C, each pair of square brackets delimit one method call. In your example there are two method calls: one to the objectAtIndex: method and one to the getValueInDollars method - thus you need two pairs of square brackets, like this:

I found a good basic tutorial about Objective-C here on Cocoa Dev Central. I recommend you work through it - it will make your life much easier.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accesing a method for a object in an array
 
Similar Threads
declaring and instantiating objects
find method to search an array
Pulling code out and making method with it instead
Problem calling a method from a different class
How to compare the elements of the array and remove the smallest item?!