View profile

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

 iOS Code Review
 iOS Code Review
Hi there,
This time I have exciting news to share - last week we crossed the 1000 subscribers mark 🥳 It’s so honouring to be able to share the learnings with such a wide community from all around the world. Thank you for tuning in!
And now, let’s dive into the tips 👇

Managing image assets
This is 100% my favourite way to organise assets - images or SF symbols, and even localisation strings.
The best part is that you can easily check in unit tests that none of the assets are missing (or misspelled). Just conform your enum to CaseIterable and test:
Asset.allCases.forEach { XCTAssertNotNil($0.image, "\($0) is missing") }
Ivan Sapozhnik
When I work with SF Symbols I usually like to put them into Asset enum and then I can either get the image directly via computed property or I can have a function that takes some custom SymbolConfiguration. Looks really clean in the code when you use it. #swiftlang #iosdev #tips https://t.co/nu2s8Xnah2
Use Labels in SwiftUI
Here’s a nice short summary of benefits of using Label over just an HStack with Image and Text. I think we should always prefer Label. More details and examples in the documentation.
#wordmeetstee
@twostraws 1. Labels can be used for simple Buttons that need text and image.
2. Or a list view item with icon and some text.
3. also has label style if you want to customise the label
4. Also supports accessibility

https://t.co/KSPTLJcpJI
On counting characters
Few issues ago I wrote about being careful not to mix ranges from String and NSString. And here’s a graphic example of how just a simple emoji is counted differently, with explanation why:
Holly Schilling
@krzyzanowskim This MOSTLY makes since.
- It’s one glyph.
- In UTF8, it’s 4 bytes which is 4 code points.
- In UTF16, it’s 2 code points which is still 4 bytes.
- NSString: Why are you like this? It’s probably returning the number of wchar_t, each of which is 2 bytes, totaling 4 bytes.
On pure functions
This is a great approach to structuring code, and it pairs well with the concept of pure functions:
Daniele Margutti
Functions that change state should not return values and functions that return values should not change state.
- B. Meyer
Visual logging
I started using emojis in logs only recently, and it’s life changing on large projects. For log levels, or just easily finding that one print statement you just added.
Curtis Herbert
Your emoji of choice for the various log levels? I’ll go first.

🚨 - Error
⚠️ - Warning
🧐 - Info
🔍 - Debug + Verbose

Makes your logs so much easier to glance. https://t.co/vh31SPooad
Memory management with async/await
This article was widely shared so you may have already seen it, but if not - definitely check it out if you’re already using concurrency. The biggest takeaway: we have to remember to cancel forever-running observations, such as for await x in in publisher {...}.
Memory management when using async/await in Swift | Swift by Sundell
🤘
Alright, that’s it for today. 
I have a question for you: I’m sending this newsletter around lunch time in the European timezone. How has your experience been with that? Share your thoughts by replying to this email or by pressing the buttons below.
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. 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.