4 min read

Issue #59

Hi there, welcome to the 59th issue of iOS Code Review. I hope you find something interesting for you today ☺️

💎 Mobile DevOps Health Check - the first industry benchmarking tool

How well does your Mobile DevOps team perform? Take the quick Mobile DevOps Health Check to find out how mature your team is and how you compare against the industry’s top-performing apps.

Take the 6-question Health Check

Swift Algorithms - split an array into chunks, and more

If you google "how to split an array into chunks in Swift", you'll get a bunch of results showing how to implement a function that can split an array, called chunked().
Another example we commonly need is removing duplicate elements from an array.

However implementing these function by yourself is not ideal (and having to write all the unit tests!). Gladly many such functions are already provided by Apple, in a lesser known package called swift-algorithms.

swift-algorithms package has various functions that are not (yet) included in the standard library. As for the reason why these functions are not in the standard library, the announcement post on swift.org sheds some light:

It’s our ambition for the standard library to include a rich, pragmatic set of generic algorithms. We think the Algorithms package can help realize this goal by serving as a low-friction venue to build out new families of related algorithms—giving us an opportunity to iteratively explore the problem space and learn how different algorithms connect and interact—before graduating them into the standard library.

Aside from .chunked() and .unique(), the package contains many other useful algorithms such as:

  • creating permutations from elements in a collection
  • rotation of elements in an array
  • selecting a random part of a collection
  • getting a certain number of min/max elements
  •  iteration over tuples of adjacent elements
  • and many more

There are a number of benefits to using this package - each algorithm is thoroughly tested, peer reviewed, and improved for best possible performance, which is also documented for each algorithm.

Announcing Swift Algorithms
I’m excited to announce Swift Algorithms, a new open-source package of sequence and collection algorithms, along with their related types.
GitHub - apple/swift-algorithms: Commonly used sequence and collection algorithms for Swift
Commonly used sequence and collection algorithms for Swift - GitHub - apple/swift-algorithms: Commonly used sequence and collection algorithms for Swift

Split a list by month

The chunked() function from swift-algorithms is not only allowing to split an array into fixed-size chunks. It also has a variant that allows to provide a custom decision-making closure for when a chunk should end.
Danijela is show casing an example of splitting a list of travel destinations grouped by month. Just be wary that for large amounts of data, it's better to utilise database-backed queries.

Split your data easily and efficiently with .chunked() | Danijela’s blog
Swift Algorithms is an open source package of sequence and collection algorithms. It contains many generic algorithms found in other popular programming languages. In this post, we’ll take a look at how to implement the .chunked() algorithm to chunk an array of data and display it in your UI.

GeometryReader - Blessing or Curse?

Some swear by GeometryReader, and some hate it to the bone. Or maybe you haven't had to use it at all. Whichever it is, this comprehensive article will be useful.
The author dives as deep as one can - with benefits and drawbacks, why it works the way it works, and how to hold it right.

GeometryReader: Blessing or Curse?
A good tool is only as good as the person who wields it

setNeedsLayout() vs layoutIfNeeded()

On the topic of layouts, here's one for UIKit. You'd use layoutIfNeeded() for an immediate update, and setNeedsLayout if you don't mind your changes to be animated.

setNeedsLayout(): 
This method does not force an immediate update, instead it marks the view for the update. It triggers the update during the next update cycle. This is efficient when you have multiple changes to make.

layoutIfNeeded(): 
This method forces the view to update its layout immediately. It ensures that all pending changes, if any, are applied immediately.

Learn the magic of setNeedsLayout() vs layoutIfNeeded() with Nitin Aggarwal. | Naman Sabarwal posted on the topic | LinkedIn
Demystifying the power of setNeedsLayout() vs layoutIfNeeded() setNeedsLayout(): This method does not force an immediate update, instead it marks the view…

✌️
Alright, that's it for today! Let's spread the good code vibes ✨🧘🌈☀️
I'm curious if you found any of the tips particularly interesting - let me know by replying to this email!

Thank you to Bitrise for sponsoring yet another issue ❤️