Objective-C elegant syntax

Source: Internet
Author: User

For beginners, objective-C has a lot of confusing statements, but they are actually very elegant.

What programmers write most are functions and functions written by themselves or written by others. This article describes the elegance of objective-C from the perspective of functions.
C # and objective-C belong to the C series language. Let's take a look at the definition and call of C # functions for comparison.
C # Function Definition:

Public void doit (string actorname, string moviename, int timesseen)
{
Console. Write ("{0} is my favorite actor in the movie {1}, I saw it {2} Times.", actorname, moviename, timesseen );
}

Function call:

Class1 objmovie = new class1 ();
Objmovie. doit ("Leonardo DiCaprio", "dreamspace", 120 );

 Let the. NET programmer look at the definition of objective-C:

-(Void) doit :( nsstring *) actorname moviename: (nsstring *) value timesseen: (INT) times {
Nslog (@ "% @ is my favorite actor in the movie % @, I saw it % I times.", actorname, value, times );
}

If you read objective-C for the first time, you won't be able to figure out the above Code and wonder if it is wrong.

For the above function definition:
1. '-' indicates that this function is an instance function (similar to a non-static function). '+' indicates that this function is a class function (similar to a static function)
2. Void indicates that this function has no return value.
3. The function name is 'doit: moviename: timesseen: ', not 'doit'
4. parameters are separated by Spaces
5. The parameter type is enclosed in brackets.
6. The parameters are divided into internal parameters and external parameters, such as the movie name. The internal parameters are: value, and the external parameters are: moviename.

7. A function parameter does not have the External Parameter Name and has an internal parameter name. For example, actorname.

Call:

From the code above, we can see that apart from the first parameter, other parameters can be added with external parameter names for difference.

From the above we can see that objective-C and C # are very different, which will be confusing to. Net programmers. The elegance of objective-C function design is that there are internal parameter names and external parameter names. You do not need to define variables internally to store function parameters.

Summary:This article is a simple summary of objective-C function learning.

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.