Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Object-C in the preparation of multi-parameter function parameter is omitted     - Linux Workstation Security Checklist - from the Linux Foundation Internal (Linux)

- 29 practical examples Linux system / network administrator of nmap (Linux)

- Servlet life cycle works (Programming)

- PostgreSQL 9.3.5 database installation under Ubuntu Server 14.04 (Database)

- Linux kernel panic (because glibc result) Repair (Linux)

- Four levels to deal with Linux server attacks (Linux)

- Replace font under Linux (Linux)

- C ++ two second pointer memory model (two-dimensional array) (Programming)

- Nginx is used to build the cache module srcache_nginx (Server)

- Linux learning portal: CentOS 6.4 system kernel upgrade (Linux)

- Docker improve safety (Server)

- Python Basics Tutorial - lambda keyword (Programming)

- Hadoop 2.7.1 installation configuration based on availability QJM (Server)

- Install Jetty on CentOS / RHEL 6.X (Server)

- To configure and use multiple Tomcat under Linux environment (Server)

- RealVNC Server 5.2.3 Installation and Configuration In Fedora (Server)

- Common data structures and functions of Linux process scheduling (Programming)

- Implement Oracle dynamic registration of non-standard port 1521 (Database)

- Install Git on CentOS (Linux)

- View processes and threads under Linux (Linux)

  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.


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


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.
- Ubuntu 14.04 install Sublime Text 3 plug and use SublimeClang (Linux)
- The Hill sorting algorithm (Programming)
- Installation GitLab appears ruby_block supervise_redis_sleep action run (Linux)
- ElasticSearch basic usage and cluster structures (Server)
- Vi editor Introduction (Linux)
- Management and application Oracle external table (Database)
- Approach the next Linux shared interrupts (Linux)
- Ubuntu 12.04 / 14.04 users to install software LyX document processing (Linux)
- Ubuntu 10.10 install Oracle 10g Installation Guide (Database)
- Ubuntu install Liferea news subscription software (Linux)
- Django template inheritance child (Programming)
- Shell Scripting Interview Questions (Programming)
- Docker Build a Java compiler environment (Linux)
- Elasticsearch Kibana installation notes (Linux)
- XtraBackup achieve non-stop use of master-slave synchronization service (Database)
- Good wireless network security information spread in the air (Linux)
- Linux VMware virtual machine after the cloning of the card can not start to solve (Linux)
- Analysis: Little Notebook facing a major security threat secure online (Linux)
- Automated Password Generator: Linux under a special password generator (Linux)
- Git uses Details (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.