Invocation of a method in Objective-c

Source: Internet
Author: User



The method of invoking a class or instance (object) in a specific language in the OC language is called a Send message or method call.



There are two ways to invoke methods in OC:



The first type:


    • [Class name or object name Method name];




[ClassOrInstance method];
 

[ClassOrInstance method: arg1];
 

[ClassOrInstance method1: arg2 method2: arg2];
 

[[ClassOrInstance method: arg1] otherMethod]; // Send messages nested 





The second type:


    • Object name. Method name; (dot syntax)




An understanding of how a method in Objective-c passes multiple parameters.


The multi-parameter transfer method in OBJECTIVE-C syntax is often the easiest place for beginners to get sleepy. I have just realized to share with you.


Analysis


Because our language experience tells us that the definition method is:


One type matches one parameter (dynamic languages can even ignore types)


For example:


C / C ++

     public void say (char * word1, char * word2)

2. JAVA

     public void say (String word1, String word2)

3. C # (exactly the same as Java)

     public void say (String word1, String word2)

4. VB

     Public function fun1 (word1 as string, word2 as string) as string

5. JS is a dynamic language that does not require types, so only parameters need to be defined

     function say (word1, word2)

6. Php

     function say ($ word1, $ word2)

7. Python (types are not required for dynamic languages, similar to JS)

     def say (word1, word2)

8. ActionScript (no parameters required for dynamic languages)

     function say (word1, word2)

9. Perl (curiosity found out this too)

     sub say {
         ($ word1, $ word2) = @_ // @ _ stands for parameter name
     } 
Objective-c
How is Objective-C defined?

     -(void) setWord1: (NSString *) word1 setWord2: (NSString *) word2

The Chinese grammar explanation is this:

     (Data return type of method) Method name: (Parameter 1 data type) Variable name of parameter 1 Parameter 2 name: (Parameter 2 data type) Variable name of parameter 2 ...


Let's look at "Parameter 2 Name: (Data Type of Parameter 2) Variable Name of Parameter 2".
* Key point: method name equals name 1

Do you understand here?
Conclusion
One parameter of the OC function is composed of 3 parts. The name of the first parameter defaults to the method name. If you take it as the parameter name, you will find that this is a loving "family of three". But why is it so defined? Because the method is called in OC:

     [obj setWord1: "Parameter 1 value", setWord2: "Parameter 2 value"]

     [Object method name: parameter 1 value, parameter 2 name: parameter 2 value] 


This is why the function parameter definition of OC is so 2.



Invocation of a method in Objective-c


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.