3 min read

Issue #49

Hi there, welcome to the 49th issue of iOS Code Review! I hope you enjoy reading through today's collection 💌

Setapp offers a collection of 240+ trustworthy, advanced paid apps under one subscription. It takes away the pain of looking up, comparing, buying, and managing separate apps.

Try Setapp for free

The efficiency of +=

The += operator, when performed a non-trivial number times, is much faster than addition and assignment with +.  (the Twitter integration is temporarily out of commission, click on the image to read the thread)

Ranged line limit in SwiftUI

We can specify a number of lines as a range, and SwiftUI will reserve space for the lower bound - even if the text itself takes less space (available from iOS 16)

Roman (@ramzesenok@mastodon.social)
Attached: 1 image - Day 21Setting line limit in #SwiftUI means setting its upper limit. Text will render as many lines as needed for content but no more that the limit. But we can also set a range as a limit. Now it will reserve space for the lower bound and render no more than upper bound lines

Accessibility for custom SwiftUI views

Whenever we make a fancy custom control in SwiftUI, we don't have to spend time making it accessible by itself - instead we can use the .accessibilityRepresentation modifier. Whenever VoiceOver navigation is used, SwiftUI will use the view created by this closure instead of our custom view. By using a system control in this representation, we save on effort while users will be happier 🤩

Roman (@ramzesenok@mastodon.social)
Attached: 1 image - Day 16Sometimes we come up with crazy fancy controls that are very inaccessible. We can try making it accessible here and there or we can also replace them with something very simple yet completely accessible. Here comes `accessibilityRepresentation` in play

Custom scopes in Swift

I love this trick and use it quite a lot. Every set of curly braces is a scope - meaning that variables declared inside it are not available on the outside. Naturally, functions, closures, and if statements are scopes, but no one is stopping us from creating scopes whenever we want - the easiest way to do that is with a do {} syntax.
Why, you ask? This allows to isolate some intricate logic, when you don't want to move out a piece of code to a separate function, but still want some extra separation inside the funciton. Last time I used it for calculating frames inside layoutSubviews.

Roman (@ramzesenok@mastodon.social)
Attached: 1 image - Day 13Ever wanted to somehow scope a portion of logic from the rest? You can use `do {}` for that. Scoping can help you limit lifetime and visibility of some variables resulting in cleaner code and faster app

The importance of code ownership

Most of important and successful projects, even though they started off small, go through different phases that turn a proud shiny code into a piece of cryptic code everyone fears and despises.
In this article you'll read about how and why it happens, and find recommendations on making sure the code is properly owned - and thus maintained.

The Importance of Code Ownership
Well-owned code is well-maintained code

✌️
Alright, that's it for today!
I'm curious if you found any of the tips particularly interesting - let me know by replying to this email!

* this issue contains an affiliate link to Setapp