View profile

 iOS Code Review | Curated code improvement tips - Issue #14

 iOS Code Review
 iOS Code Review
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
Sufiyan Yasa
Added word count filter feature for SMS blocker.
On counting word - most blogs suggest white space separation. Bad idea. Not all languages ( ex ja ) has spaces.

Instead: use CFStringTokenizer
This article deserve more claps: https://t.co/vnUEqLFHIC

#buildinpublic #iOSDev https://t.co/pVRDCn18jx
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.
The Developer 
Hey iOS developer ,

Make sure, you're using "yyyy" and not "YYYY" in your date format strings. Typically you want to use "yyyy" to represent a four-digit calendar year (e.g. "2021"), while "YYYY" represents a so called 'week year'

Example : https://t.co/m7T1N88o9V
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(_:):
Donny Wals 👾
Quick tip: Use DateFormatter's setLocalizedDateFormatFromTemplate to easily get a formatted date string that matches your user's locale #Swift #iOSProgramming https://t.co/3SqsOHdh3B
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
GitHub - russell-archer/IAPDemo
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
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 🙌
Did you enjoy this issue? Yes No
 iOS Code Review
 iOS Code Review @ios_code_review

Bi-weekly newsletter amplifying code improvement tips from the Apple developer community in a bite-sized format. Swift, Objective-C, iOS, macOS, SwiftUI, UIKit and more. Curated by Marina Gornostaeva and published every other Thursday.

In order to unsubscribe, click here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Created with Revue by Twitter.