What's New in SwiftUI State Management

Поделиться
HTML-код
  • Опубликовано: 8 июн 2023
  • In this in-depth RUclips video, we delve into the realm of state management in SwiftUI, exploring three essential concepts: @State, @Bindable, and @Environment. Whether you're a beginner or an experienced SwiftUI developer, understanding these techniques is crucial for building robust and efficient user interfaces.
    Want to learn about SwiftData framework? Enroll in my brand new course "SwiftData - Declarative Data Persistence for SwiftUI".
    Coupon Code (Expires 06/14): www.udemy.com/course/swiftdat...
    Referral Link (If above coupon expires):
    www.udemy.com/course/swiftdat...
    📌 @State: We kick off the video by exploring the power of @State, an essential property wrapper that enables us to manage and mutate local state within a SwiftUI view. We'll learn how to use @State to create dynamic and reactive UI elements that respond to changes in user input or other factors.
    📌 @Bindable: As we progress, we dive into the intricacies of @Bindable, a property wrapper that allows us to create custom bindings for data properties. By leveraging @Bindable, we can effortlessly update UI components based on changes in specific model properties, leading to more flexible and synchronized user experiences.
    📌 @Environment: Finally, we uncover the versatility of @Environment, a property wrapper that grants access to shared data across multiple views. With @Environment, we can propagate values through the view hierarchy without passing them explicitly, streamlining our code and promoting reusability.
    #swiftui #swiftdata #iosdev #iosdeveloper Check out my courses at
    azamsharp.school/

Комментарии • 24

  • @gregdurfee1915
    @gregdurfee1915 10 месяцев назад +1

    This kind of presents a novel use of SwiftData; SwiftData use was offered at WWDC 2023, mostly as simpler alternative to CoreData. What you've shown in this video is an example of abstraction reaching back from ContentView to Room, the child view, to the Light class, which holds the persistent value of isOn. The user action is taken in ContentView, the @Bindable variable is an instance of what used to be the ObservableObject. This design pattern is easy to read. Brilliant!

  • @martygo
    @martygo 17 дней назад

    Useful video. Thanks.

  • @smotch7533
    @smotch7533 Год назад +5

    I was hoping this would be a what changed and not a beginner tut, there are so many of those

    • @donathmm3881
      @donathmm3881 Год назад +3

      Yeah... He is talking so slowly and so i had to speed up to 3x

  • @designckinet
    @designckinet 2 месяца назад

    Useful information ℹ️ thanks

  • @StewartLynch
    @StewartLynch Год назад +3

    @Binding is still used if you do not want to pass an entire Observed object into your child view and only want to bind a single property from that object, or another @State property from the parent view

    • @azamsharp
      @azamsharp  Год назад

      Yup! I hope Apple don't remove Binding. I don't particularly like that everything is moving towards reference type like Bindable and Model.

  • @angrypi
    @angrypi Год назад +3

    I am a little confused about the @bindable.
    three questions:
    1. why doesn’t @bindable work with value type? in apple’s documentation, @Observable can use both is reference types and value types.
    2. what’s the relation ship between @bindable and @binding?
    3. when child view uses @bindable, why isn’t the view initialized with the `$` prefix like using with @binding?

  • @mario_luis_dev
    @mario_luis_dev 3 месяца назад

    you only need to import `Observable`. The import `SwiftData` also includes `Observable`, but you’re introducing overhead by importing SwiftData when the only functionality you want to use is in Observable.

  • @bobwint8114
    @bobwint8114 4 месяца назад

    I didn’t read thru all the comments but just in case this fix is not there…..there is a fix for needing that superfulous struct to support @Bindable. Simply add the stmt “@Bindable var light = light” before the Toggle that was in the Room struct (i.e. put the Toggle back) and then you don’t need the LightView struct

  • @indiekiduk
    @indiekiduk Год назад +1

    I think it's important to know that @State's initializer is now using an autoclosure (same as StateObject) which is why it is no longer a memory leak to use classes as @State.
    @frozen @propertyWrapper public struct State : DynamicProperty {
    ...
    @available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *)
    public init(wrappedValue thunk: @autoclosure @escaping () -> Value) where Value : AnyObject, Value : Observable

  • @metzkarl8767
    @metzkarl8767 5 месяцев назад

    nice video. Could you please make a tutorial on how to use the Observation framework with CoreData? Thx

  • @angelilton
    @angelilton 6 месяцев назад

    When I pass environment to contentView its like use contextApi from react ? I came to react native and to try to learn swiftui

  • @cinnamonrage
    @cinnamonrage Год назад

    Good video but chapters would help in future ones.

  • @geildesignt
    @geildesignt Год назад

    Is this #Preview new too?

    • @azamsharp
      @azamsharp  Год назад +1

      #Preview is a macro. Yes it is new.

    • @geildesignt
      @geildesignt Год назад

      @@azamsharp Thanks :)

  • @codelabspro
    @codelabspro Год назад

    Why do they keep changing the SwiftUI state and property wrapper APIs and syntax every single time? It makes no sense.

    • @sacredgeometry
      @sacredgeometry Год назад

      Because its still not right.

    • @eldarsadykov
      @eldarsadykov 10 месяцев назад

      They don't. @Observable is not a syntax change, it's a new feature of the framework.

  • @sebnoumea
    @sebnoumea Год назад +3

    When using the new Environment property wrapper, you don't have to go through a new View to use a Binding. You just have to declare a new var using the new Bindable PW like so
    var body: some View {
    @Bindable var light = light
    Toggle(isOn: $light.isOn, label: {})
    .fixedSize()
    }

    • @azamsharp
      @azamsharp  Год назад

      I created the Gist of what I want to do. I want to access and change the Environment object from without the subview.
      gist.github.com/azamsharp/3df44f211413e58e736b08456ea500f7

    • @azamsharp
      @azamsharp  Год назад

      Thanks! I got your Gist. Yes adding Bindable works as you pointed out. Thanks! But I am not too happy with the fix as it requires another variable to be created. I think it might be a bug in SwiftUI that it does not allow updating the Environment directly because it allowed to do that before.

    • @sebnoumea
      @sebnoumea Год назад +2

      @@azamsharp you'll need to file a feedback so that a projectedValue is added to @Environment