Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Object-C in the preparation of multi-parameter function parameter is omitted     - Install the latest Pinta graphics editing software on Ubuntu 14.04 (Linux)

- MongoDB slice simple example (Database)

- Java interface and inheritance (Programming)

- How do you change the default browser and Email Client in Ubuntu (Linux)

- How Datadog monitor Nginx (Server)

- Use XtraBackup be physical standby database MySQL (Database)

- Build Docker based MongoDB replication cluster environment (Database)

- MySQL restart process can not be taken lightly (Database)

- Ubuntu 14.04 + xRDP + Xfce implement Windows Remote Desktop Connection (Linux)

- Linux System Getting Started Learning: The Linux ac command (Linux)

- Linux Tutorial ---- fundamentals of data traffic redirection (Linux)

- The formatted Linux hard drive and mount (Linux)

- CentOS7 method to upgrade the kernel to 3.18 (Linux)

- SSH Filesystem use a secure connection for network file system (Linux)

- Boost-- time and date - (1) timer library introduction (Programming)

- Linux Getting Started tutorial: build your own Vim (Linux)

- HTTP Client Hints Introduction (Server)

- Java Virtual Machine Basics (Programming)

- Debian (Wheezy) were installed wxPython GUI development (Linux)

- Java look and feel mode (Facade mode) (Programming)

 
         
  Object-C in the preparation of multi-parameter function parameter is omitted
     
  Add Date : 2018-11-21      
         
       
         
  In Object-C, we often encounter an indefinite number of parameters of functions such as NSLog (NSString * format, ...). When a function such as how to achieve it? This blog will give you the answer.

Quote:

In Object-C, we will encounter a lot of functions such as NSLog, wherein the number of parameters of uncertainty, freedom is controlled by the programmer, widely used in the initialization of the array, dictionary, etc., then, is how to achieve such a function it? How we write our own function parameter is omitted it? Of course, this is not the only method for processing multi-parameter function, you can also pass through a dictionary or array parameters. But this gives us a mechanism C, is undoubtedly the most convenient.
 
First, understand several concepts

va_list

A pointer to the C language is defined, for pointing to the current parameter.

va_start (ap, param)

This macro is a list of initialization parameters, where the first parameter is the va_list object and the second argument is the first argument of the argument list.

va_arg (ap, type)

Macro, this first argument for taking a parameter of the macro is the va_list object and the second parameter is the parameter type to be removed.

va_end (ap)

This macro is used to fetch the parameter list Close

Second, take the principle of multi-parameter function parameters

Before writing our own multi-parameter function, to understand the function of the Senate to take the principle is important, first of all, function parameters are placed on the stack segment of our memory, but also put forward order is placed from, For example, if a function parameters are as follows:

void func (int a, int b, int c, int d)

So when passing parameters to the stack parameters d, then c, b, a. Thus so, taking the reference time, according to the principle of the value of the stack, the value of the order of a, b, c, d. So in principle, as long as we know the type of address of the first parameter and each parameter, we can take out the parameters. The macros described above, is to help us to do these.
 
Third, the statement omitted parameters and implementation of multi-parameter function

"..." This is what we used to achieve symbol symbols omit a parameter. For example, we simulate a log function to achieve the following:

- (Void) myLog: (NSString *) str, ... {// wording omitted parameters
    va_list list; // create a pointer to the object list
    va_start (list, str); // initialize tabulating, str as the first argument is omitted before ... and before that parameter
    NSString * temStr = str;
    while (temStr! = nil) {// if not nil, the argument continues
        NSLog (@ "% @", temStr);
        temStr = va_arg (list, NSString *); // return value to take, and let the address pointer to the next parameter
    }
    va_end (list); // close the list pointer
}

Note that when you call, we must add the condition nil in the final end of this judgment parameters:
 
[Self myLog: @ "312", @ "321", nil]; // must have nil
 
Fourth, add one point


Careful you may have found here is nil when we call the function manually add, but many functions in the system we call the system directly to help us add a parameter at the end of the nil, for example,

 NSArray * array = [NSArray arrayWithObjects: (id), nil]

This is how to do it? We only need to add a macro statement in the function, you can achieve this functionality, the following:
 
- (Void) myLog: (NSString *) str, ... NS_REQUIRES_NIL_TERMINATION {// add a macro here
    va_list list;
    va_start (list, str);
    NSString * temStr = str;
    while (temStr! = nil) {
        NSLog (@ "% @", temStr);
        temStr = va_arg (list, NSString *);
    }
    va_end (list);
}

As the name suggests, this macro is the role of the end position plus nil we need.
     
         
       
         
  More:      
 
- CentOS 6.5 can not connect to the network under VMware (Linux)
- Valgrind * not * leak check tool (Linux)
- Linux common network tools: hping Advanced Host Scan (Linux)
- Generate two-dimensional code parsing code Java (Programming)
- How to deploy Icinga server (Server)
- Easily create RPM packages using the FPM (Linux)
- Oracle EBS R12 for Linux installation (Database)
- iOS development -Launch Image and Launchscreen (Programming)
- Linux (SUSE) mount NTFS mobile hard practice (Linux)
- MongoDB simple replication configuration (Database)
- Use Python to perform system commands common usage (Programming)
- awk pattern matching (Programming)
- Wildcards and special symbols usage comments under Linux (Linux)
- Analysis of MySQL High Availability (Database)
- Linux port mapping system (routing and forwarding) (Server)
- Java how to achieve bubble sort the problem Arraylist (Programming)
- Cacti Linux-based system monitoring and alarm (Linux)
- mysqldump implement database logical backup (Database)
- WordPress blog installation Redis Cache (Server)
- About Auto Layout and frame (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.