[Reprint] A small example introduces the function naming of obj-c

Source: Internet
Author: User

Original link: http://www.cnblogs.com/liufan9/archive/2013/04/02/2995626.html

For a former C # or Java development friend, the first contact with iOS development, Obj-c function naming method may feel very unaccustomed.

Especially when you open APPDELEGATE.M, the code you're greeted with is:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{    Override point for customization after application launch.    return YES;}

I do not know whether there will be friends and I had the same feeling--oh, this is too long!!!

In fact, in Obj-c, the function name of the function above should be:

-(BOOL) Application:didfinishlaunchingwithoptions:

The application and launchoptions followed by parentheses are the parameters used within the function.

We can interpret this function as "application: Complete startup Using option:".

Where the parameter after the "application" colon is the name of the application itself.

The parameters after the "Complete startup using options" Colon are the name of the startup option.

Here I used two "as the name implies", right is the name as a definition! The way the Obj-c function is named is a bit like natural language, I'm sure you'll be like me, and with the gradual deepening of iOS development, you will become more and more comfortable with and even like this naming method.

For example, if we define an addition function in Java, we will probably use the following method:

    /**     * Calculated a+b Results     * @param a     * @param b     * @return return a+b and */public    int Add (int a, int b) {        retur n A + b;    }

When defining a function in Java, we usually get used to writing a simple comment, so that we can use the smart hints from eclipse to see what each of these parameters mean in other places in the program, and you can also use the Javadoc tool to generate the program documentation. This is very convenient and meaningful!

And how do we do it in the obj-c? First define a function with the following code:

-(Nsinteger) AddA: (Nsinteger) a WITHB: (Nsinteger) b {    return a + b;}

When calling the function, Xcode's hints are as follows:

[Self AddA: (Nsinteger) WITHB: (Nsinteger)];

At this point, we can interpret this sentence as "plus A and B", have you found it easier to understand some ah? It should be, hehe.

In Obj-c, if you develop a good function naming habit. Believe that as time goes on, you will find that there will be less and fewer comments to write. Although there is no smart hint like eclipse in Xcode, it does not prevent us from seeing at a glance what each function does and what parameters they need, and what each parameter should be.

Come and enjoy a fun iOS development trip ~ ~ ~

[Reprint] A small example of how obj-c functions are named

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.