IOS Development Learning SUMMARY objective-c object-oriented method

Source: Internet
Author: User

IOS Development Learning SUMMARY objective-c object-oriented method

Objective-c methods cannot exist independently. All methods must be defined in the class. Logically, a method belongs to either a class or an object.

Attribute of the Method

It is not difficult to find that methods are similar in syntax and function. In fact, methods are developed from traditional functions.
Objective-c uses the same parameter passing mechanism when calling a function as when calling a method. They both pass values and are copies of input parameters.

Methods are significantly different from traditional functions:

In structured programming languages, functions are first-class citizens. The whole program is composed of one function;
In object-oriented languages, classes are first-class citizens. Therefore, methods in OC cannot exist independently and must belong to Classes or objects.
Since methods in objective-c cannot exist independently or be executed as independently as functions, classes or objects must be used as callers when executing methods.
From above, the attributes of methods in objective-c are mainly reflected in the following three aspects:
1. methods cannot be defined independently and can only be defined in the class body.
2. methods are logical, belong to either a class or an object.
3. Methods in objective-c cannot exist independently or be executed as they are functions. classes or objects must be used as callers when executing methods.

Variable number of parameters

When defining a method, add a comma and three (,…) after the last parameter name (,...), It indicates that the parameter can accept multiple parameter values.
To obtain a variable number of parameters in the program. Use the following keywords:
-Va_list: This is a pointer variable used to define a list of variable parameters.
-Va_start: This is a function. Specify to start processing the list of variable parameters and point the pointer variable to the first parameter in the Variable Parameter List.
-Va_end: stop processing variable parameters and release pointer variables.
-Va_arg: This function returns the value of the parameter currently pointed to by the pointer and moves the pointer to the next parameter.

Example program:
Header file: VarArgs. h

# Import
  
   
@ Interface VarArgs: NSObject // method for defining a variable number of parameters-(void) test :( NSString *) name,...; @ end
  

Implementation file: VarArgs. m

# Import VarArgs. h @ implementation VarArgs-(void) test :( NSString *) name ,... {// use va_list to define an argList pointer variable pointing to the variable parameter list va_list argList; // if the first name parameter exists, you need to process the parameter if (name) {// because the name parameter is not in the variable parameter list, you must first process the name parameter NSLog (@ % @, name ); // set argList to the first parameter in the first variable parameter list and start to extract the parameter va_start (argList, name) in the Variable Parameter List ); // va_arg is used to extract the parameter currently pointed to by the argList pointer and move the pointer to the next parameter. // arg is used to save the currently obtained parameter. If this parameter is not nil, enter the cyclic body NSString * arg = va_arg (argList, id); while (arg) {// print each parameter. NSLog (@ % @, arg); // extract the next parameter again and move the pointer to the next parameter arg = va_arg (argList, id);} // release the argList pointer, end extraction va_end (argList) ;}@ endint main (int argc, char * argv []) {@ autoreleasepool {VarArgs * va = [[VarArgs alloc] init]; [va test: @ crazy iOS handout, @ crazy Android handout, @ crazy Ajax handout, nil] ;}}

Essentially, this variable parameter is also an array-like structure. It should be pointed out that a variable number of parameters can only be placed at the end of the list of parameters. That is, a method can have at most one variable-length parameter.

 

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.