Variable parameters of objective-c syntax

Source: Internet
Author: User

The methods of variable parameters are not uncommon in objective-c, and many common methods like cocoa are variable parameters, such as:

1 NSLog (NSString *format, ...) 2 + (ID) arraywithobjects: (ID) firstobj, ... 3 + (ID) Dictionarywithobjectsandkeys: (ID) firstobject, ...

So how do we implement our own method of change, in fact, we need to use the C language about the variable parameter of a set of macros:va_list,va_start,va_arg,va_end, the following example is the implementation of a variable parameter method:

Main.m

1 #import<Foundation/Foundation.h>2 #import "ChangeableParameterClass.h"3 intMainintargcConst Char*argv[]) {4 @autoreleasepool {5[Changeableparameterclass Method:@"Testing Variable Parameters"6Withauthorname:@"Kenmu"7Withchangeableparameter:@"Firstparameter",@"Secondparameter",@"Thirdparameter",@"Fourthparameter",@"Fifthparameter", nil];8     }9     return 0;Ten}

ChangeableParameterClass.h

1 #import<Foundation/Foundation.h>2 @interfaceChangeableparameterclass:nsobject3 /**4 * Test variable parameters5 *6 * @param name7 * @param authorname author name8 * @param the first parameter element of the Firstparameter variable parameter, ",..." means that there may be more than one parameter element later (the variable parameter must be the last parameter of the method, ending in a ",..." way)9 */Ten+ (voidMethod: (NSString *) name Withauthorname: (NSString *) AuthorName withchangeableparameter: (NSString *) Firstparameter,...; One @end

Changeableparameterclass.m

1 #import "ChangeableParameterClass.h"2 @implementationChangeableparameterclass3+ (voidMethod: (NSString *) name Withauthorname: (NSString *) AuthorName withchangeableparameter: (NSString *) Firstparameter,... {4NSLog (@"%@,%@", name, authorname);5     6Va_list list;//pointer to variable parameter list7Va_start (list, firstparameter);//use the first parameter to initialize the pointer to a list8NSLog (@"strcurrent=%@", Firstparameter);//Strcurrent=firstparameter9      while(YES) {TenNSString *strcurrent = va_arg (list, NSString *); One         if(!strcurrent) { A              Break; -         } -NSLog (@"strcurrent=%@", strcurrent);//strcurrent=secondparameter ... the     } -Va_end (list);//to end the acquisition of a mutable parameter - } - @end

Like most variable parameter methods, you must add nil to the end, because this group of macros does not provide a measure of the number of parameters, of course, you will ask why NSLog parameters we do not have to add nil at the end of the argument, that is because the first parameter of NSLog is a formatted string, With this note string you can get the number of arguments behind, so if your method can also have other parameters to explicitly indicate the number of arguments, of course you can also write (but in the method body needs to be modified to call Va_arg by known number), but I still recommend the above!

Variable parameters of objective-c syntax

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.