View profile

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

 iOS Code Review
 iOS Code Review
Hi there,
Hope you enjoy today’s collection 🙌

Updating @State values
It’s not safe to update @State properties from any callbacks except those provided by the SwiftUI framework. Always use View.onReceive(_:perform:) to react to external changes and update the view state. Below is the example of how to subscribe to NotificationCenter notifications:
Alex Brown
@twostraws @azamsharp @state @State values may only be updated safely in SwiftUI callbacks such as button actions and onReceive.

If you try to handle updating @state in the callbacks from other async APIs like combine or NotificationCenter directly, they randomly succeed or fail in subtle way (see log).
@twostraws // Subscribe to notifications using Combine

Text("Hello, world!")
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
// Get the notification when the app enters foreground
Disabling animation on a View
.animation(:_) modifier was deprecated in iOS 15, so now to disable animations we should use .transaction(_:) modifier that lets us customise the transition between two states. The doc says: “Use this modifier to change or replace the animation used in a view.”
Antoine v.d. SwiftLee 
How would you disable animations in SwiftUI on a specific view? I used the animation(nil) modifier but found out it was deprecated in iOS 15.

#swiftlang #iosdev
forEach vs for
There are some things a for loop can do that forEach can’t, and vice versa. for offers a more fine-grained control flow, allowing to exit early with break, as well as to iterate conditionally with for ... where ... . On the other hand, forEach allows to pass closures to it. In most cases though it simply comes down to personal taste!
Marco Eidinger 🧑🏻‍💻
Ben Cohen from the Swift Core Team about forEach 📣

"I wish it didn’t exist. People use it arbitrarily instead of for…in for no apparent good reason ... Just use a for loop."

What is your opinion about forEach in Swift?

Jason Mitchell 
for loops and forEach are very similar in #SwiftLang, but not exactly the same… ⁦@johnsundell⁩ explains the differences in this article

Ben Cohen
“your reduce would be better as a for loop”
jk! reduce is nice sometimes.
On (unnecessary) optimization
Bee O'Problem
You read some code and find two algorithms.

One is O(n!) time (very slow)

The other is O(n) (not so slow)

So you fix the O(n!) but oops. It ran only once per level and n was, like, 3 at most. Meanwhile the O(n) had an n of 20000 every frame. You wasted your time

Profile first
The Hidden Costs of Your Dependencies
Ever had to argument why adding a third party dependency to the codebase does not come for free? Here’s an amazing concise article summarizing all the different aspects:
The Hidden Costs of Your Dependencies | Jason Zurita
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 in a bite-sized format. Swift, Objective-C, iOS, macOS, SwiftUI, UIKit and more. Curated by Marina Gornostaeva and published every other Thursday.

For feedback or sponsorship ->> [email protected]

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.