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.
🤯 Optional view
Optional conforms to View as long as its Wrapped associated value conforms to View.
This means we can provide nil from ViewBuilder block. All the below declarations are valid.
🤯 some keyword is not required
some View is an opaque result type introduced in Swift 5.1 which conforms to View.
If you take a look at View protocol, Body is an associated type.
This means we can specify the return of body function with any concrete type conforming to View
🤯 Make a custom resultBuilder
The some keyword for opaque result type ensures returning a specific concrete type conforming to the protocol.
Returning dynamic Shape to some Shape is not valid. However, we can make our own ShapeBuilder, the same way ViewBuilder does.
🤯 ViewBuilder under the hood
The convenient SwiftUI syntax we have is powered via ViewBuilder, so in a sense we can use ViewBuilder function directly, or declare explicitly with the underlying type like TupleView.
• • •
Missing some Tweet in this thread? You can try to
force a refresh
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.