3 min read

Issue #48

Hi there, welcome to the 48th issue! Today you'll learn about initializing views, sorting arrays of strings, modern UISegmentedControl, and setting up git-ignore for your project. Enjoy! 🌞

💎 Sponsor of the week - Bitrise

What happens when you survey 1600+ Mobile DevOps teams? You get 5 key Mobile DevOps benchmarks that can guide mobile teams to high performance. Download our free report and start setting goals for your team based on industry-recognized benchmarks.

Download free report

Lightweight view initialization

Initializer of a SwiftUI view should be as lightweight as possible, as it is executed many times. In fact, it's a good rule of thumb for UIView and UIViewController initializers too - keep the heavier work for the moment when the view loads or appears.

Sorting user-entered strings

Don't just use .sorted() on arrays of strings. A quote from localizedStandardCompare(_:) documentation:

This method should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate. The exact sorting behavior of this method is different under different locales and may be changed in future releases. This method uses the current locale.

By the way, Swift's String implementation is so great, that it already treats special characters correctly during equality checks with ==. Using the right comparing method brings the same greatness to sorting of strings 🙌

An example from the Swift docs about equality with diatrics:

The letter é can be represented as the single Unicode scalar é (LATIN SMALL LETTER E WITH ACUTE, or U+00E9). However, the same letter can also be represented as a pair of scalars — a standard letter e (LATIN SMALL LETTER E, or U+0065), followed by the COMBINING ACUTE ACCENTscalar (U+0301). The COMBINING ACUTE ACCENT scalar is graphically applied to the scalar that precedes it, turning an e into an é when it’s rendered by a Unicode-aware text-rendering system.

... Two String values (or two Character values) are considered equal if their extended grapheme clusters are canonically equivalent.

... LATIN SMALL LETTER E WITH ACUTE (U+00E9) is canonically equivalent to LATIN SMALL LETTER E (U+0065) followed by COMBINING ACUTE ACCENT (U+0301). Both of these extended grapheme clusters are valid ways to represent the character é, and so they’re considered to be canonically equivalent

Read the article from the tweet to learn about more ways of sorting arrays of strings.

Modern UISegmentedControl

Lovely modern API for reacting to UISegmentedControl selection, that doesn't require @objc methods anymore:

Avoiding junk in the git diff

Inside each .xcodeproj and .xcworkspace file there is a folder called xcuserdata, containing files that are individual to each user who opens the project - that users breakpoints, saved search scopes, and other configurations. xcshareddata folder contains files belonging to the whole project such as schemes and the Package.resolved file.
💡 xcuserdata folder should be git-ignored, to avoid polluting the git tree and commit history, especially on projects with many contributors.

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