当前位置: 动力学知识库 > 问答 > 编程问答 >

ios Objective C why does a method with no name work

问题描述:

This question already has an answer here:

  • Empty method name, what does this actually do?

    2 answers

网友答案:

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.

网友答案:

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

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

网友答案:

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.

(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.

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: