File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

Accesing a method for a object in an array

Anders hofel

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"];

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.
I agree. Here's the link:
subject: Accesing a method for a object in an array
It's not a secret anymore!