Utility functions in Swift

Tram Ho

  • Sometimes the only way to influence the common state of the code base make major changes, such as changing the code architecture, changing the way data is transmitted, or applying new framework .
  • In this article, we’ll look at how to write small utility functions that make common tasks easier with simpler pattern to use.

1 / Configuration closures:

  • Part of the code in projects is dedicated to identifying certain object to use. Especially when using object orient UI like UIKit. Just as we have gone through Encapsulating configuration code in Swift , finding neat ways to write separate code can improve the transparency of the logic as well as the extent to which the code is used.
  • For example, we are using the common pattern using self-executing closures as follows:

  • There is nothing wrong with the above code, but it is better if we can reduce the amount of code associated with each property . There are a number of different approaches we can take here such as using factory methods instead of self-executing closures let’s see if we can write a simple utility funtion to help us code It is more compact while still using the same pattern .
  • Introduce to you the function called configure , it will take a value that we want to configure and closure to do the job configure it. We can encapsulate all of our configure code and also pass a remote parameter with the inout keyword that allows our new function to be used with value types:

  • Now we can go back to our HeaderView and make the configure code subview more readable:

  • One advantage of our configure code function is that it can be used with any type from UIView to any type of object .
  • For example, we use the same pattern as above when setting the URLRequest value to be used to synchronize data when users are connected to WiFi:

2 / Rethrowing functions:

  • One detail of the above function that can easily be missed is that it is marked with the keyword rethrow . What keywords do is to tell the compiler Swift only as functions that throw if the closure is passed to it as well throw .

  • Whenever we design any AP I that accepts synchronous closures , we should definitely consider marking our functions with re-access times that allow us to throw error when needed.

3 / Reducing boilerplate:

  • Besides specifying code convention utility function can also help us avoid common errors such as performing more specific tasks, such as defining layout and color .

  • We can choose something as simple as the extension UIView automatically prepares the given view for use with auto layout then activates a range of constraint :

  • With just that small extension , we can really make our source code more readable:

  • Let’s look at another example we looked at to easily identify dynamic coloe adapting to whether the user’s device is currently running in light mode or dark mode :

  • We are trying to determine which color pairs to use in light mode or dark mode . Name our new colorPair function and let it accept a UIColor to use for s light mode or dark mode . We will then call the UIColor API to return the appropriate color for each UIUserInterfaceStyle :

  • We declare our backgroundColor above as follows:

  • Combining the above feature with another utility function allows us to identify any UIColor using the dot syntax:

  • While writing utility function like the above, we may ask ourselves questions like why are the default API designed this way? As with almost everything in programming – great APIs are often about balancing a certain set of tradeoffs.
Share the news now

Source : Viblo