2 min read

Issue #14

Hi there,

Welcome to the first issue of 2022! ☀️

Hope everyone had lovely holidays. I did, and am pumped to jump back into sharing tips regularly. Enjoy!

On counting words

There are multiple problems with splitting words by punctuation and whitespace - even in English "shouldn't" would split into two 'words'. And some languages don't even have spaces. Full article: Three Ways to Enumerate the Words In a String Using Swift by @sorenlind

Working with years in dates

Are you using YYYY or yyyy in your date formats? YYYY represents week year which is the same as calendar year most of the time, but isn't for some dates at the end of December. So there's plenty of time to fix it 😀 Good resources on this:

https://nsdateformatter.com to quickly test different formats. This site also has a nice documentation page where you can get a refresher on which APIs to use when. Or check out the official Unicode documentation if you want to get to the original.

Custom date formats, localized

Setting DateFormatter's dateStyle and timeStyle  result in it producing localised strings. To get a custom set of date components as a localised string, we can use setLocalizedDateFormatFromTemplate(_:):

Learnings from SwiftUI APIs

This is how you set a custom title view in the navigation bar in SwiftUI. How would one ever guess that this is how it should be done? ToolbarItemPlacement.principal  has a small paragraph about its effect. In my opinion, this API is not discoverable at all. (thanks @sarunw for sharing this!)

Two learnings here: 1) SwiftUI is still cool and powerful, you can do so much with so little code 2) With great flexibility of API, the understandability can take a hit. Because it will behave differently in different contexts and different platforms, SwiftUI has to use overly generic terms such as 'toolbar' or 'principal placement'. So users have to resort to documentation for understanding what it even means. This is a big tradeoff and something to keep in mind when designing our own API.

StoreKit resources

Stumbled onto two repos with comprehensive information on working with StoreKit APIs. There is a repo for StoreKit 2 (new in iOS 15) and a repo for the good old StoreKit. If you're looking to implement in-app purchases from scratch, these are great as reference after checking the official Apple documentation.

Both repos have a mile-long readme with every little detail covered, as well as working code that covers every edge case - unlike the official samples provided by Apple. Made by @archer_russel

GitHub - russell-archer/StoreHelper

Implementing and testing In-App Purchases with StoreKit2 in Xcode 13, Swift 5.5 and iOS 15.

GitHub - russell-archer/IAPDemo

Implementing and testing In-App Purchases in Xcode 12 and iOS 14, including local receipt validation.

Review PRs in IDE form

Did you know that you can review PRs in Github in a full-blown IDE right in the browser? I find it more convenient, being able to navigate between files and having extra code highlighting. You can even make edits. Inline PR discussions are included. Press `.` (dot) while on the PR page, or change the url from GitHub.com to GitHub.dev.

VS Code in the browser


Alright, that’s it for today.

Did you enjoy this issue? Let me know by pressing the buttons below.

Got feedback? Want to see more, or less of certain kinds of tips? I’d love to hear from you. Simply reply to this email, or reach out on Twitter via @ios_code_review 🙌