View profile

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

 iOS Code Review
 iOS Code Review
Hi there,
It’s getting harder and harder to not share cool things from iOS 16 (that we can’t yet use). I’m managing to resist so far. Hope you find something interesting and useful in today’s collection!
By the way, if you enjoy reading the newsletter - send it to a colleague ☺️

Using unsafe pointers safely
Unsafe pointers and bytes are only “safe” to use inside the body closure. From withUnsafeMutablePointer documentation: The pointer argument is valid only for the duration of the function’s execution.
Ben Cohen
var unsafePtr = array.withUnsafeMutableBufferPointer {$0}

^ never ever ever ever do this ever

“withXXX” in Swift means “only use $0 inside this block”. If you return $0 from the block the gremlins will come and find you eventually and will make you very sad.
DRY, evolved
Check out this thread about SPOT - Single Point Of Truth principle. It’s similar to DRY (Don’t Repeat Yourself), but better. I’ve been looking at code this way as long as I remember - I had no idea this way of thinking had its own name.
This comment perfectly points out the difference between SPOT and DRY:
Alex Kuiper
@adamgordonbell That's actually really good! It distinguishes between conceptually the same and accidentally the same.
Beautiful rounded corners
Are you using cornerRadius for rounding corners? There’s a better way - .clipShape as a RoundedRectangle with continuous style in SwiftUI produces a smoother looking result. To achieve the same in UIKit, do button.layer.cornerCurve = .continuous .
Check this thread for comparison and SwiftUI code:
Vadim Belyaev
(1/9) How to make beautiful rounded corners in #SwiftUI, a thread: https://t.co/7MRuQR5ULm
CloudKit
In issue #21 I shared an article about CloudKit being a red flag for indie developers. The biggest issue named was the inability to transfer the app to another developer account, if a CloudKit entitlement was ever used in the app.
That restriction is now gone - now apps that use iCloud can be transferred to another developer account 🎉
Here’s the updated list of criteria for app transfer: App Transfer Criteria
App Store Connect Update - Releases - Apple Developer
Non-optional Core Data attributes in Swift
There is a distinction between the concept of optional and non-optional values in Swift and Core Data. In Swift, you get compile-time guarantees, but in Core Data, the framework is enforcing the model rules in run-time. Optional Core Data attributes should be optional properties in Swift, but what about non-optional attributes?
In this article Jesse shares a robust approach to handling the differences and making sure the app doesn’t crash.
🤘☀️☮️
Alright, that’s it for today. 
Did you enjoy this issue? Let me know by pressing the buttons below! If you enjoyed it, you can help grow the newsletter by spreading the word ☺️
Got feedback? Want to see more, or less of certain kinds of tips? I’d love to hear from you - just reply to this email 👋
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.