A small example to introduce Obj-C function naming

3991 단어 function
For friends who have done C# or JAVA development before, when they first come into contact with iOS development, the function naming method of Obj-C may feel very unaccustomed.
Especially when I open AppDelegate.m, the code that catches my eye turns out to be:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    // Override point for customization after application launch.

    return YES;

}

I don't know if there will be friends who feel the same way I did - oh my God, this is too long! ! !
In fact, in Obj-C, the function name of the above function should be:

- (BOOL)application:didFinishLaunchingWithOptions:

The application and launchOptions following the parentheses are the parameters used inside the function.
We can interpret this function as "application:finished launch using options:".
The parameter after the "application"colon is, as the name suggests, the application itself.
The parameter after the colon in "Complete Startup Using Options", as the name suggests, is the startup option.
Here I use two "as the name suggests", yes, as the name suggests! The way of naming functions in Obj-C is a bit similar to natural language. I believe that everyone, like me, will become more and more comfortable with or even like this naming method with the gradual deepening of iOS development.
Let's take another example. If we define an addition function in java, it will probably be as follows:

    /**

     *  a+b 

     * @param a

     * @param b

     * @return  a+b 

     */

    public int add(int a, int b) {

        return a + b;

    }

When defining functions in java, we are usually used to writing a simple comment, so that we can use eclipse's smart prompts to see what these parameters mean in other places in the program, and we can also use the Javadoc tool to generate program documentation . This is very convenient and makes sense!
And how would we do it 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's prompt will look like this:

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

At this point, we can interpret this sentence as "add A and B". Do you think it is easier to understand? It should be, hehe.
In Obj-C, if you develop good function naming habits. I believe that over time, you will find that there will be fewer and fewer comments to write. Although there is no intellisense similar to eclipse in Xcode, this does not prevent us from seeing at a glance what each function does, what parameters they require, and even what types of parameters should be.
Let's enjoy a happy iOS development journey together~~~

좋은 웹페이지 즐겨찾기