Call the objective-C method

Source: Internet
Author: User

After writing the class declaration and implementation, how can an application call it?

In objective-C, the simple format of calling a method is as follows:

[Instance method]; for example, [person setage: 32]; where person is an instance of the person class.

Or:

[Class Name and method name]; for example: nsstring STR = [nsdate date]; this is to directly call the method date in the nsdate class to obtain the current date and time.

In objective-C, calling a class or an instance method is also called sending a message to this class or instance ). Class instance is called "receiver ". Therefore, the format of common method calls can also be understood:

[Receiver message];

In terms of terms, the entire expression is also called a message expression. This is the official name.

Of course, a method may have parameters or multiple parameters. Therefore, the complete method call format is as follows:

[Receiving Method Name sub-1: parameter 1 sub-2: parameter 2 sub-3: parameter...]

For example:

[Person setage: 32];

[Person setname: @ "Sam" andsecondname: @ "job"];

Note: When calling Multiple Parameter methods, you can omit the method name starting from the second. 

For example:

[Person setname: @ "Sam": @ "job"];

You can call another method in a method, for example:

[Nsstring stringwithformat: [test format];

Note: To call a method, you must add the brackets "[......]".




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.