Basic Objective-C syntax

Source: Internet
Author: User

Objective-CBasicSyntaxIs the content to be introduced in this article, for beginners,Objective-cThere are a lot of confusing ways to write, in fact they are very elegant. What programmers write most isFunctionAnd calls self-written or written by othersFunction. This article starts fromFunctionFrom the perspectiveObjective-c.

C # AndObjective-cIt belongs to the C series language. Let's take a look at C #'sFunctionDefine and call.

C # Function Definition:

 
 
  1. public void doIt(string actorName, string movieName, int timesSeen)  
  2. {  
  3. Console.Write("{0} is my favorite actor in the movie {1}, I saw it {2} times.", actorName, movieName, timesSeen);  

Function call:

 
 
  1. Class1 objMovie = new Class1(); 

ObjMovie. doIt ("Leonardo DiCaprio", "dreamspace", 120 );

Let the. net programmer look at the definition of objective-c:

 
 
  1. - (void) doIt:(NSString *) actorName movieName: (NSString*) value timesSeen: (int)times   
  2. {   
  3. 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.

We can see from the aboveObjective-cIt is very different from C #. net programmers.Objective-c FunctionsThe elegance of the design is that internal parameter names and external parameter names are both available, so you do not need to internally define variables to store them.Function.

Summary:Objective-CBasicSyntaxThis article is aboutObjective-c FunctionsFinally, I hope this article will help you!

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.