Fully updated for Xcode 11.2
SwiftUI relies heavily on property wrappers to make our code easier to read, write, and maintain, but if you’ve never used them before you’d be forgiven for wondering where all the
$ signs have come from – they can seem alien at first.
Although property wrappers are a general Swift feature since they were introduced in Swift 5.1, they are particularly common in SwiftUI – you’ll see
@EnvironmentObject and many more, all with the purpose of helping reduce the amount of boilerplate in our code.
We’ll be going into detail on each one of SwiftUI’s property wrappers in the next few chapters, but just briefly it’s worth summarizing the basics:
@Statelets us modify properties inside a struct.
@FetchRequestexpects you to have placed a Core Data managed object context into the environment ahead of time.
@ObservedObject @Binding var value = SomeClass()is not allowed.
@EnvironmentObject!) they are all different and it’s important you use them appropriately.
That covers the basics, but to really understand how these property wrappers work it’s worth investigating them individually.
SPONSORED Instabug helps you identify and resolve severe crashes quickly. You can retrace in-app events and know exactly which line of code caused the crash along with environment details, network logs, repro steps, and the session profiler. Ask more questions or keep users up-to-date with in-app replies straight from your dashboard. Instabug takes data privacy seriously, so no one sees your data but you! See more detailed features comparison and try Instabug's crash reporting SDK for free.