View profile

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

 iOS Code Review
 iOS Code Review
Hi there,
Happy Xcode RC week! The release notes are extensive, but nonetheless are worth checking at some point, because there are many small improvements that are not mentioned anywhere else. I’ll be going through the changelog myself, and will post anything that can improve our code 🙌
Let’s dive in 👇

Crash in Combine
It’s almost a tradition now to start the newsletter with a tip about an obscure Combine issue… This time, it turned out that breakpointOnError() crashes apps. To solve this, you can make an extension that conditionally uses this only if debugger is attached. But I just comment it out completely when not debugging 😅
Marina Gornostaeva ✨
TIL not to use Combine's breakpointOnError() in production builds.
It crashes the app when not attached to a debugger 😬
SwiftUI + Combine = 🧡
SwiftUI’s onReceive opens some really interesting possibilities. Here’s @danielsaidi with an example showcasing the power of SwiftUI when you sprinkle extra Combine on top. This code subscribes to a data binding of a UI element (a form in this case) and pipes changes into a save function. Makes the UI code really concise and pretty hard to break.
Daniel Saidi
When building forms in #SwiftUI, you can easily implement autosave when a manual save isn't desirable. This view extension takes a custom @Published publisher and executes a custom save operation whenever the item changes. You can also specify a custom throttling interval. https://t.co/ZYAjtDKefH
Swift API design guidelines
Saw a tweet the other day, a person saying that they wish Swift had an official API guideline. So a refresher just in case - it does! Makes you a pro at writing function signatures that feel right at home in Swift.
Lazy local variables
Starting with Swift 5.5 (shipped with Xcode 13), we can use the lazy keyword on local variables. Code that had to use closures to achieve this lazy behaviour can now be simplified 🎉
Code example from the release notes
Code example from the release notes
Time-based git diffs
Git supports a bunch of ways to reference commits (revisions): by commit hash (full or short), branch name, HEAD~n to jump back n commits, etc. Apparently you can not only reference commits, but also reference a specific moment in time. Thanks to @trevorbrindlejs for sharing this.
If you want to learn about all the ways to reference revisions, check the documentation.
Trevor Brindle
Today I learned:

You can see time based comparisons in git (and in github at github​.​com/${owner}/${repo}/compare)

Use this syntax for the branch name:

`[email protected]{2hours}`
`[email protected]{10days}`
`[email protected]{1month}`

`git diff [email protected]{2hours}...latest` https://t.co/gQCylm7ssT
🤘
Alright, that’s it for today. 
Did you enjoy this issue? Let me know by pressing the buttons below, so I can improve the newsletter. 
Got feedback? Want to see more, or less of certain kinds of tips? I’d love to hear from you. 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. 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.