Introduction to variable parameter functions in Swift _swift

Source: Internet
Author: User

A variable parameter function is a function that can accept any number of arguments, and what we are most familiar with is probably the NSString-stringwithformat: method. In Objective-c, the way we use this method to generate strings is this:

Copy Code code as follows:

NSString *name = @ "Tom";
NSDate *date = [NSDate Date];
NSString *string = [NSString stringWithFormat:
@ "Hello%@." Date:%@ ", name, Date];

The parameters in this method can be arbitrarily varied, and the first item of the argument is a string that needs to be formatted, followed by a blank in the first parameter. Here we no longer describe in detail the variable parameter functions in Objective-c (this is a Swift book, after all), but I believe that most readers with a few years of objective-c experience will find it hard to write a function that accepts variable parameters without consulting the data.

But all this has been simplified as never before in Swift. Now, a function that writes a variable parameter only needs to be added after the type when declaring the parameter ... It's OK. For example, the following declares an INT additive function that accepts variable parameters:

Copy Code code as follows:

Func sum (input:int ...)-> Int {
//...
}

Input inputs will be used as array [Int] inside the function body, let's do the above method. Of course you can add the traditional for...in, but here we've chosen a way to look more swift:
Copy Code code as follows:

Func sum (input:int ...)-> Int {
Return Input.reduce (0, combine: +)
}

println (sum (1,2,3,4,5))
Output: 15


When using variable parameters, it is necessary to be aware that the variable parameters can only be used as the last parameter in the method, rather than declaring a mutable parameter before declaring another parameter. This is easy to understand because the compiler will not know where the input parameters should be truncated. Also, in a method, there can be at most one set of variable parameters.

A more annoying limitation is that variable parameters must be of the same type, and we need to make some modifications when we want to pass in multiple types of arguments at the same time. For example, the first mentioned-stringwithformat: methods. The first element of a variable argument list is a string waiting to be formatted, which corresponds to a string type in Swift, and the remaining arguments should be any type corresponding to the formatting standard. One workaround is to use any as the parameter type and then dock the first element of the received array for special processing. However, because Swift provides an external label with an underscore _ to be used as a parameter, it is no longer necessary to add a parameter name to the call. We can take advantage of this feature when declaring the method is to specify that the first argument is a string, and then follow an anonymous argument list, so that when it is written, "as if" all parameters are processed in the same parameter list, it will look much better. For example, Swift's nsstring format statement is handled in this way:

Copy Code code as follows:

Extension NSString {
Convenience init (format:nsstring, _ Args:cvarargtype ...)
//...
}

The call is almost the same as it was when objective-c, very convenient:
Copy Code code as follows:

Let name = "Tom"
Let date = NSDate ()
Let string = nsstring (format: "Hello%@. Date:%@ ", name, date)

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.