1

By mistake I noticed that this work:

on interface MyTest.h

- (void):(int)idIndex;

on implementation MyTest.m

- (void):(int)idIndex {}

and I can even call it,

[self :2 ];

so why im I allowed to use a method with no name?

sorry if is a strange question, is just very strange behaviour in my opinion, cheers

manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216

3 Answers3

5

The method has a name, it is :.

Not very readable though, is it?

It catches out programmers new to Objective-C that the colons are part of the method name.

Abizern
  • 146,289
  • 39
  • 203
  • 257
2

why im I allowed to use a method with no name?

Who says the method doesn't have a name? The name is :.

Caleb
  • 124,013
  • 19
  • 183
  • 272
-1

By definition a function in just a memory address where to jump to for the program to continue executing code. parameters are placed on the stack for the function to retrieve. Therefore a function does not need a name other than to make it easy for a developer to call.

As far as a developer is concerned, functions are located by their name. Therefore a function without a name has little to no use to a developer. This is why providing a meaningful name is important.

That being said the fact is that the methods are quite similar to other languages in the fact that the entire name is the function name, and the parameters are placed on the stack for the function to retrieve. therefore the function's name is not really split into sections to place the parameters in the middle of, rather that is for the convenience of the developer to be able to read the command from left to right and determine what each parameter is. Naming these functions is equally important.

A function for instance that is called -[ClassType addInt:toInt:] used as such

[myObj addInt:10 toInt:12];

would be the same in another language as

[ClassType addInt:toInt:]( myObj,10,12 );

by using the parameter type for the function, you can tell what is supposed to be in each parameter just by reading from left to right, If for instance the command was

-[myObj addDouble:toInt:]

you would immediately know that the first parameter was a double, and the second parameter was an int.

you can see that this is a correct analog for this in the macintosh terminal by using the nm command

nm /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation | grep "NSArrayM"

This will show you the exported functions in the CoreFoundation.framework that refer to the NSArrayM object.

here is the output from the above command.

nm symbol output

(I used NSArrayM to limit the list and display a correct analog, but all of the commands in CoreFoundation are there for you to look at if you remove the pipe into grep.

The first parameter of any function that starts with a - is the object address, whereas the functions that start with a + require no object address as they are static functions.

Update

In the interest of accuracy, I did some research on Selectors and the objc_sendMsg() function.

Selectors not being the same as Functions or Methods, I delved deeper into how they work. And As it turns out, the idea of functions holds true. While a string is used to determine the selector either at compile time, or at runtime. It is provided as a GUID that uniquely identifies the selector to the application. That GUID is put into a lookup table for each object that responds to the selector. Therefore the Selector in the instance of this question is ":", however the function that is called from that selector could very well have no name at all.

Here is a link to a stackoverflow answer that eventually brought me to that understanding.

Understanding uniqueness of selectors in Objective-C

As well as a link to the Source for objc_sendMsg

http://www.opensource.apple.com/source/objc4/objc4-371/runtime/Messengers.subproj/objc-msg-i386.s

and an image of the specific function in the assembly code.

objc_sendMsg source code

I apologize if this has confused anyone, I hope this clears things up, and that others find this as interesting as I did.

Community
  • 1
  • 1
The Lazy Coder
  • 11,560
  • 4
  • 51
  • 69
  • *a function does not need a name...* This is somewhat misleading. Obj-C method names aren't just a convenience for programmers -- they're used to find the method implementation during the [dispatch process](http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map/). – Caleb Jan 08 '14 at 06:08
  • Well it is correct. A function is literally a jump command with a set of parameters waiting on the stack. Ill try to update to make that clearer. – The Lazy Coder Jan 08 '14 at 06:09
  • Also. While objective c may use the name as the message called to a specified object, a functions definition does not change. Also the fact that objective c is written on top of c/c++ it has the same functionality. Therefore if I know where a function is located, I can easily provide a signature and a function address and call it as if I knew the name, even if no name exists. Thank you however for the information, I do want my answers to be correct and understandable. – The Lazy Coder Jan 08 '14 at 06:13
  • The question is about methods and their names, not functions, so I think it's only fair to operate at that level. Yes, you can get the implementation of a method and call that directly using a [runtime method](http://bit.ly/1bPPNsq), but to do that you need a selector, i.e. the name. All that aside, I think what you've written here isn't incorrect but misses the point of the OP's question, which is simply surprise that `:` is a valid method name. – Caleb Jan 08 '14 at 06:29
  • While I appreciate your input, your outlook on the terminology is incorrect. Method an Function can be used interchangeably. May I point you to another stack overflow answer that points out the minor differences. http://stackoverflow.com/questions/155609/what-is-the-difference-between-a-method-and-a-function#155655 – The Lazy Coder Jan 08 '14 at 06:36
  • That's a fine answer, but it deals with 'method' in a more general OOP sense and isn't specific to Objective-C. The implementation of an Obj-C method is a function with [a specific form](http://stackoverflow.com/a/4847477/643383). Messages are mapped to the corresponding implementation using the method selector (again, name) at run time. – Caleb Jan 08 '14 at 07:29
  • Your disagreement has been noted. However this is an Answer site. Therefore a detailed explanation can be most helpful to those wishing to understand how programming works. I wish I could convey to you how incorrect you are, but it is apparent that your whimsical answer provided above is detailed enough for you. I did not feel that it was adequate, And I felt that providing both background and insight was more helpful in this situation. knowing that the name is `:` and understanding why names are chosen are two different things. Please dont make this into an argument. – The Lazy Coder Jan 08 '14 at 07:35