Khoa 🔥 Profile picture
AppStore App of the Day @PastePalApp 🏆 Also made @GetAlmighty @wwdctogether @ChatJoyAI @CodelimeHQ @ShipmunkApp @IndieGoodies

Jul 27, 2022, 8 tweets

SwiftUI helps make apps faster, and I've started to use it in all my production apps

Here are some #SwiftUI tips I've learned along the way that will help you write better SwiftUI apps ✨✨

✨ There is a strokeBorder method

Some shape like Circle conforms to InsettableShape protocol, which provides strokeBorder method.

The stroke method strokes inside and outside the border.
The strokeBorder insets the stroke by lineWidth / 2

✨ Take advantage of Publisher in Combine

Both State and StateObject attaches to the lifetime identity of a SwiftUI view.

In cases where we need to adjust the stream, I prefer using ObservableObject. Here is how I debounce the Textfield text to perform searching.

✨ Be aware of missing dot

I'm sure we have accidentally run into this code a few times.

The code below looks good with no compiler warning but will crash the app at runtime.

Here background modifier is applied to ContentView instead of the avatarView, hence the infinite loop.

✨ Optional conforms to View

Do you want to conditionally show the background only?

I see people use Color.clear for empty case, but remember that Optional also conforms to View if its Wrapped conforms to View.

So we can just write nil instead.

✨ Use continuous rounded corner

With RoundedRectangle, you can specify continuous style to have that "squircle" shape that is more smoothly rounded and looks much more natural.

✨ Ensure data type matches for tag

Picker selection depends on the tag with matched data type to manage the selected state.

For simplicity, I usually just conform enum itself to Identifiable.

✨ Bindable list binding syntax

From Xcode 13, it allows bindable list binding syntax to create binding for each element inside a list.

Before this, we had to make custom binding and manually update back the original list.

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling