View profile

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

 iOS Code Review
 iOS Code Review
Hi there,
It’s that Thursday again 🥳 I found so many gems in the last few weeks, that today’s issue contains more tips than usual. I also re-tweet additional tips throughout the week that didn’t make it into the newsletter, so make sure to follow @ios_code_review for even more tips.

❤️ SwiftUI previews
SwiftUI previews can be very handy for other use-cases besides previewing SwiftUI views. 
You can use previews to preview UIKit views, which can save you dozens of hours in rebuilding to see your changes:
GitHub - theoriginalbit/PreviewView: Make use of SwiftUI Previews for UIKit!
SwiftUI previews can also be used to preview app themes and other code-only visuals such as generated images:
Marina Gornostaeva ✨
💡 SwiftUI tip:

SwiftUI previews can be handy not only for previewing app's views.
Here I'm using them to preview themes for my app.

Even cooler fact: you can use this even if the app itself is not built with SwiftUI!
On manipulating strings
Swift’s strings are unicode-compatible in the best way, counting the real displayed graphemes as the user would see them.
We need to be careful though when working with NSString APIs, because they deal with unicode differently. In the example below, print((word as NSString).length) would print 5 , not 4. So be careful not to apply ranges returned from NSString-based APIs to String instances.
Pitt (Swift && Tips)
It doesn't matter the total number of Unicode scalar values to build a character. Swift will only "count" the final character result.

Learn more about Strings and characters here:
❤️ SF Symbols
SFSymbols are really powerful these days. They support thickness, styling and even multiple colors. What you see here on the screenshot is achievable with just native APIs on iOS 15. For an overview on how to use SFSymbols in SwiftUI and UIKit, check out The Complete Guide to SF Symbols by Paul Hudson. There’s also an official Mac app for browsing and previewing all available symbols.
Unit testing retain cycles
Sometimes it’s useful to test not only what a type does functionally, but also that there are no retain cycles and an instance deallocates as expected. This can be useful for view controllers and other complex classes dealing with closures and dependencies, where retain cycles are common.
Here’s a short snippet by @ZiadeMarcus for verifying that an instance has deallocated after a test. It uses addTeardownBlock which is pretty neat on its own and can be used for other purposes too.
Assert failure messages
We write unit tests with the intention that they pass, but they really help us when they fail and catch errors in our code. So we should strive to have useful failure messages that hint at what’s wrong. Not all assert failures provide insight out of the box - @basthomas gives an overview of commonly used XCTAssert* functions and how their failure messages are reported:
A couple of debugging tricks
In iOS 15 you can do po [tableView _rowData] in the debugger to print the internal geometry (e.g. heights, y-offsets for each row, etc) of the table view. This can be useful to track down why things look the way they do. For Swift, it would be po tableView.value(forKey: "_rowData”)!. This tip was shared by @smileyborg.
Another trick is to print app’s view controller hierarchy with po [UIViewController _printHierarchy]:
Guillaume Algis
TIL about [UIViewController _printHierarchy], which prints the whole ViewController hierarchy of your app. Very useful in lldb!

(Use `expr -l objc++ -O -- [UIViewController _printHierarchy]` if stopped in a Swift frame)
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.