[Reprint] a small example introduces the Obj-C function naming method.

Source: Internet
Author: User

[Reprint] a small example introduces the Obj-C function naming method.

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

 

For a friend who used to develop C # or JAVA, the function naming method of Obj-C may not be used to iOS development for the first time.

In particular, when you open AppDelegate. m, the following code appears:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // Override point for customization after application launch.    return YES;}

I don't know if my friends will feel the same way as me. It's too long !!!

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

- (BOOL)application:didFinishLaunchingWithOptions:

The application and launchOptions following the brackets are parameters used in the function.

We can interpret this function as "Application: complete start use options :".

The parameters following the colon of "application" are the application itself.

The parameters after the colon "complete start use options" are the startup options as the name suggests.

Here I use two "as the name suggests", right as the name suggests! The function naming method in Obj-C is a bit similar to the natural language. I believe that, like me, with the development of iOS, you will become more and more familiar with and even like this naming method.

The following example shows how to define an addition function in java:

/*** Calculate the result of a + B * @ param a * @ param B * @ return returns the sum of a + B and */public int add (int a, int B) {return a + B ;}

When defining a function in java, we usually get used to writing a simple annotation, so that we can see what these parameters mean by using the smart prompts of eclipse in other places of the program, you can also use the Javadoc tool to generate program documents. This is very convenient and meaningful!

What should we do in Obj-C? First, define a function. The Code is as follows:

- (NSInteger)addA:(NSInteger)a withB:(NSInteger)b {    return a + b;}

When calling this function, Xcode prompts the following:

[self addA:(NSInteger) withB:(NSInteger)];

At this point, we can interpret this sentence as "adding A and B". Do you think it is easier to understand this sentence? Yes, huh, huh.

In Obj-C, If you develop a good naming convention for functions. I believe that over time, you will find that less and less comments need to be written. Although there are no smart prompts similar to eclipse in Xcode, this does not prevent us from seeing at a glance what every function is doing and what parameters they need, and even what type of parameters should be.

Enjoy a happy iOS development journey together ~~~

 

Related Article

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.