• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Accesing a method for a object in an array

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic