Swift Extensions

WWDC 2018 recently concluded and though there was no new hardware, no talk about the HomePod, etc. There was a special emphasis on Developers – both Code and Tools. Swift is now becoming the defacto language for Apple. However it has come to a point where there are many core Objective-C devs that feel Swift is not a good option over Objective-C to the point that one user is converting all swift examples/code to Objective-C.

There is some truth behind this, Swift has the potential and has excited many developers as an easy to pick and start development type language. However instead of becomd an easy to use language like Lua or Python and have syntactic sugar like Corona, Gideros or even more like Moai (all of which are Lua based). The reality at the point is that one cannot simply take swift and write a console utility like you could in Python or even Perl. The problem is that Swift, the language as it is remains an evolving project that takes the good parts from other languages, however to be useful to make apps it still relies on the frameworks like Cocoa/CocoaTouch.

This will not get resolved overnight or in the next release, this requires a deeper more focus approach by Apple to understand that they are now in the position that Microsoft was in between 2005-2010. They had just killed off VB6 and were pushing their devs towards VB.net or C#, a lot of devs petitioned for bringing back something that was easier, they kept adding functionality that they felt were important stating that they have added these based on feedback by the dev community. It is similar here in the case of Apple, the devs that require syntactic sugar and easier to use API’s are far more than the few that make the decision on the direction of the language. While this remains an issue that many are pulling out their popcorn and watching this unfold.

Till then, let’s look at some cool stuff that we can do with Swift

Creating an array with a number of elements

Swift allows us to create an array with pre-determined elements (literal arrays) and specially if we want to create an array with repeating elements, it is even easier. Consider the code

let numberArray = [1,1,1,1,1,1,1,1]

This can be easily created by using the code

let numberArray = Array(repeating: 1, count: 8)

This is easier and more readable. Readable code is the new point of focus that devs must look at

Reducing for Boolean values

There is a possibility that as a dev, you might come across using the reduce function at some point. Most of the examples generally speak about how you can use reduce to sum all the numbers in the array, or process strings. Another little known can be processing Boolean values. To that effect, consider the code below,

var contains = true
for number in numberArray {
  if number != 1 {
    contains = false
    break
  }
}

This iterates through the array and if the number is not 1 then it sets the contains variable to false.
Now consider the following code using reduce, like

let containsReduce = numberArray.reduce(false, {$0 || $1 == 1})

The entire loop is now in one line, it is compact code but I think it reduces readability. After a while you can easily decipher what it does.

We can use this methodology to validate if ALL the contents of the array satisfy a particular condition. This is a feature that is in the new version of Swift (as of this time). This can be created as an extension on Array as so,

extension Array where Element == Int {
  func satisfiesAll(_ condition: (Element) -> Bool) -> Bool {
      return self.reduce(false, {$0 || condition($1)})
  }
}

Now you can use the same as,

let canSatisfy = numberArray.satisfiesAll({$0 % 2 == 1})

This ensures that all the elements in the array are odd numbers.

Some kinks to iron out

You might have noticed the function in the extension uses where Element == Int . This is required otherwise the compiler complains. I am not sure how to check for multiple protocols so this can be set for Int and String type arrays. Maybe a follow up post might have a solution to that problem.

Views All Time
Views All Time
1259
Views Today
Views Today
1
Posted in Article, Tip.