Updated for Xcode 12.5
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 might wonder where all the @
and $
signs have come from – they can seem quite 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 @Published
, @ObservedObject
, @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:
@State
lets us modify properties inside a struct.@FetchRequest
expects you to have placed a Core Data managed object context into the environment ahead of time.@ObservedObject @Binding var value = SomeClass()
is not allowed.@Environment
and @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 ViRE offers discoverable way of working with regex. It provides really readable regex experience, code complete & cheat sheet, unit tests, powerful replace system, step-by-step search & replace, regex visual scheme, regex history & playground. ViRE is available on Mac & iPad.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.