Hi there, welcome to the 59th issue of iOS Code Review. I hope you find something interesting for you today ☺️
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
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 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.
.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.
Split a list by month
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.
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.
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.
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.
This method forces the view to update its layout immediately. It ensures that all pending changes, if any, are applied immediately.
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 ❤️