Here are 5 tips you can start using today π
Hi π
Itβs the beginning of a new week and I want to start it by sharing with you 5 tips that you can start using today!
#01 β Number Formatters for Social Features
If you ever work on an app with social features, thereβs a good chance that youβll need to display numbers with this format: 12,3K or 32M.
Turns out thereβs no need to write any custom code to achieve this result, because iOS already contains a built-in formatter just for this:
#02 β Shorthand if-let syntax
Last year Swift introduced a new shorter syntax to unwrap optionals:
if let optional {
// β¦
}
Whatβs pretty great is that you can update your entire project quite easily, just by running a couple regular expressions!
#03 β Localized Accessibility Labels of Images
Apple has always put a strong emphasis on making accessibility as simple as possible to implement and this tip is a perfect example of it.
You donβt actually need to update your code to provide an accessibilityLabel() for a SwiftUI Image!
Instead you can simply add a new entry to your Localizable.strings file, with the name of the image as the key π
#04 β Multiple Cursors in Xcode
This is one of my personal favorite trick in Xcode!
By using the shortcut ββ β Control + β§ - SHIFT + Clickβ you can create multiple cursors, and itβs so useful when youβre refactoring or writing boilerplate code!
#05 β Dealing with iPhone and iPad Layouts
When you work on an app that supports both the iPhone and the iPad, itβs quite common for the layout of the UI to be slightly differentdepending on the device.
Itβs usually little things, like a font size or the width of a cell. But if weβre not carefully, we can easily end up with UI code thatβs littered with ifstatements, just to deal with these small differences π
And thatβs where this 5th tip comes into play! Because this problem can be elegantly solved just by introducing a simple helper function:
Bonus β Handling Optionals in Tests
To finish, I also wanted to share one of my personal tips with you!
If you need to deal with optional values in your tests, thereβs a much better solution than just force unwrapping them: you can use the function XCTUnwrap() instead π
Iβll explain how it works and why itβs a better choice in the video below π
Thatβs all for this email, thanks for reading it!
I wish you an amazing week!
β€οΈ