This content originally appeared on Level Up Coding - Medium and was authored by Herbie Dodge

Functions play a vital role in programming. They allow us to define a set of instructions into modular chunks of code to be reused making our code more clean and proned to less errors. Like any programming language, Apple has built in a plethora of functions for devs to use in their projects; there are often times we need to write our own custom functions to suit our coding needs.
If you’ve done any programming/debugging in swift, chances are you’ve used Swift’s native print() function to present data to the console. This little handy function can take in many data types, but it also can take in multiple pieces of data at once.

Print() is a special kind of function known as a Variadic Function which accepts multiple data types and places them into an array to be used within the function body. The best functions solve common issues and reduce on the amount of lines code needed to complete a task, keeping us in within the DRY principal (Don’t Repeat Yourself).
Imagine we are building an application programmatically with no storyboards. We are building a sign up field for our app, asking the user to provide us with an email, full name, password, and phone number. Starting out we would create all of our properties in our form.

Our next steps would be to add our textfields to our view with the addSubView() function, then to disable auto layout so we can specify where our textFields should go. To keep our ViewDidLoad() function clean we will do this additional set up in our custom function called configureUI().

While this definitely works, it’s not the most elegant solution to reducing the amount of code in our project. Our textFields do require unique constraints when it comes to layout and design, but they do share some common requirements to get them up and running: they need to be added to our view as children so they can be placed within our view hierarchy and for programmatic layout to be enabled, auto layout needs to be disabled indicated by translatesAutoresizingMaskIntoConstraints or TAMIC for short. The commonalities with our properties can be refactored into a Variadic Function to clean up our code. So what is the syntax for one of these fancy functions?
Well its really quite simple really, to define a Variadic Function all that is required is three periods (…) after the declaration of the type being passed in. To follow a similar naming conviention to Apple’s addSubview() we will name our Variadic Function addSubviews(). The declaration of our function will be added as an Extension on UIView.

As stated before, the constant declared in the function will be an Array, in our case an Array of type UIView (just like print’s items argument is an Array of type Any). With all our textFields now grouped together in the same place, its as simple as iterating through our collection and setting our common properties on our textFields.

Parsing through our collection is as simple as setting up a for loop then adding our logic. Since this is an extension to UIView we will call addSubview to self, just like you’d call view.addSubview() in your ViewController; then we will set the view’s TAMIC property to false for auto layout. This code has considerably reduced the amount of lines of code needed in our ViewController to set up our textFields, but it is also even more modular by declaring it as an extension to UIView, allowing us to set up any object that inherits from UIView in other places within our app!Our Variadic Function logic can be reduced further using Array’s built in Map Function, along with Closure’s Syntactic Sugar.

Our new function is ready to be used back in our ViewController! To pass in our textFields (or other UIViews) all we need to do is separate each item with a comma just like an array.

Variadic Functions can reduce the amount of repeated code needed to complete a given task, making our code clean, reusable, and readable.
Variadic Functions in Swift was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Herbie Dodge

Herbie Dodge | Sciencx (2021-05-20T12:39:05+00:00) Variadic Functions in Swift. Retrieved from https://www.scien.cx/2021/05/20/variadic-functions-in-swift/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.