Syntax question in header files


#1

I am kind of perplexed what the significance of the bold part in this is: - (void)addAssetsObject:(Asset *)a

I was looking through some of the more extensive Obj C headers and saw that this came up quite a bit, so I imagine my complete understanding of it is important. What exactly is happening in this method declaration here? Sorry if this is an obvious question whose answer I somehow missed in the text.


#2

No worries. I just last night answered a similar question in this forum: viewtopic.php?f=148&t=3357

In short, everything after the colon in that line of code expresses the concept that when sending the message addAssetsObject:, you must supply an object of type Asset.

The a itself is just a placeholder. The method may or may not use a as the variable name representing the asset being added.

Does that help?


#3

Sorry to add to this question, but is (Asset *) means, adding an asset object from Asset class? ‘Asset’ pointing correct class and * means object?


#4

To be really precise: addAssetsObject: expects you to supply a pointer to an instance of Asset when you call it. The * means “a pointer to”.


#5

I try to avoid discussing pointers when talking about syntax like this because in Objective-C, it appears to be impossible to treat an object as anything other than a pointer. You can’t declare objects on the stack, for example, and the message sending is clearly tied to pointer variables.

Aaron, is there anywhere in Objective-C or Cocoa when dealing with true objects where it’s useful (or necessary) to think of them as pointers?


#6

When dealing with beginners, I’m always very specific about the fact that the variables are pointers, and that the variable holds an address where an object lives.

The distinction is very important when you reuse the pointer to point to several different objects. (This baffles people who confuse the pointer with the object.)

But, you are right: the variables are always pointers, so you never need to think about the alternative.