Khoa 🔥 Profile picture
Jul 2, 2021 6 tweets 2 min read Read on X
SwiftUI ViewBuilder is resultBuilder, and from Swift 5.3 you can declare if let, if case, multiple if statements, switch, ... pretty much like a normal block function.

ViewBuilder is recommended to leverage SwiftUI type system to ensure performance. Here are a few examples 👇
Local variables.

Pretty much like a normal function, you can declare local variables and return View at last.
Mix if and switch statements

This is handy when you want to show modal or overlay of different Views
If statement only.

No more if else with EmptyView, you can just return View for the happy path.
Empty return.

For function marked with ViewBuilder, you don't need to return anything
Return nil.

Optional conforms to SwiftUI View as long as its Wrapped conforms to View. So we can return nil from ViewBuilder

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Khoa 🔥

Khoa 🔥 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @onmyway133

Jul 27, 2022
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 Image
✨ 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. Image
Read 8 tweets
Jun 20, 2022
✨✨ SwiftUI tips

Y probably using SwiftUI in iOS 15 by now, here are 10 good practices to make good use of #SwiftUI #iosdev
✨ Take advantage of flexible frames

Sometimes we want to show a button in the corner of the screen and we use VStack, HStack with Spacer.

We can use flexible frame with maxWidth, maxHeight. It is more concise and does not have default spacing like in the stack.
✨ Use overlay with ViewBuilder

Let's say we want to show a star icon to the top right of a book cell.

Before iOS 15, overlay and background modifiers accept only View, so we need to perform logic in Group.

Now we can just specify alignment with content ViewBuilder.
Read 11 tweets
Apr 16, 2022
Are you new to iOS and Swift and want to find good blogs to learn & get up to date?

I can recommend the following 21 best free resources and blogs to learn Swift and SwiftUI for iOS developers #iosdev 👩‍💻🧑‍💻
⚡️ Stanford University's course CS193p (Developing Applications for iOS using SwiftUI)
cs193p.sites.stanford.edu

⚡️ Raywenderlich
raywenderlich.com

⚡️ ObjC Issues
objc.io/issues/

⚡️ NSHipster
nshipster.com

⚡️ 100 days of Swift
hackingwithswift.com/100
⚡️ Learn to code (Apple)
developer.apple.com/learn/curricul…

⚡️ App Dev Training (Apple)
developer.apple.com/tutorials/app-…

⚡️ Kavsoft
kavsoft.dev

⚡️ Sarun by @sarunw
sarunw.com

⚡️ Swift by Sundell
swiftbysundell.com

⚡️ Donny Wals by @DonnyWals
donnywals.com
Read 5 tweets
Apr 11, 2022
What are some new things you learn about #SwiftUI lately?

Over the course of making several SwiftUI apps, I've discovered quite a few hidden magic of SwiftUI that are quite fun.

Here are 6 interesting SwiftUI features in View Builder many don't know are even possible 🤯
🤯 View protocol with enum

Struct is not the only way to describe #SwiftUI views. Learn how you can achieve the same thing with just enum
🤯 Conform primitive type to View protocol

You can also conform any value type or primitive type to View protocol

The only requirement View needs is to yield a reasonable body.
Read 7 tweets
Apr 5, 2022
Have you recently discovered some nifty UIKit APIs that you wish you had known earlier?

Apple introduces new changes every WWDC but it may take a while until they can be adopted in production, hence developers forgot.

Here are 9 useful APIs from iOS 7 - 15 many fail to notice
keyboardDismissMode, iOS 7

Have you tried making the keyboard dismissal follow along drag gesture? It's not an easy feast but I still see manual implementation in some codebases.

They didn't know that in iOS 7, you can specify keyboardDismissMode to be .interactive. Image
NSLayoutConstraint.active, iOS 8

I know everyone should be using NSLayoutAnchor and not the tedious NSLayoutConstraint.

Before iOS 8, we needed to find the view that is the closest common ancestor of the items managed by the constraint, and it is a very tricky task. Image
Read 10 tweets
Mar 17, 2022
Development & design tools for iOS developers #iosdev ⚡️🔥

These are the 33 tools I use daily and have saved me so much time that I can't recommend them enough.
🔥 WTF Auto Layout
Demystify Auto Layout constraint error logs
wtfautolayout.com

🔥 NSDateFormatter
Preview different date formats
nsdateformatter.com

🔥 Quick Type
Convert JSON to Swift code
app.quicktype.io

🔥 Screen Sizes
Device info
screensizes.app
🔥 Cubic Bezier
Play around with bezier path
cubic-bezier.com

🔥 Regex101
Test regular expression
regex101.com
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(