Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Binding<String?> by String? for read-only access #5

Merged
merged 1 commit into from
Sep 29, 2020
Merged

Replace Binding<String?> by String? for read-only access #5

merged 1 commit into from
Sep 29, 2020

Conversation

gobetti
Copy link
Contributor

@gobetti gobetti commented Sep 29, 2020

I'm super new to SwiftUI so I may have understood it wrong, anyway I found that opening a PR might be the fastest way to answer my question. This project still works for me after these changes - my badge still updates fine if I make it a let (immutable read-only) instead of passing in a Binding. https://swiftuipropertywrappers.com seems to suggest that approach when write access isn't needed.

Are there scenarios where Binding is needed here?

Thank you!

PS.: the practical reason why I didn't want to inject a Binding was that I wanted the badge to be computed from an @EnvironmentObject instead of having yet another property (probably a @State) that replicates part of that information.

struct AppView: View {
    @EnvironmentObject var myObject: MyObject

    var body: some View {
        StatefulTabView {
            Tab(title: "Foo", systemImageName: "0.circle", badgeValue: "\(myObject.items.count)") { // SwiftUI knows it needs to update this when myObject changes, if I had to inject a Binding how would I do it without creating a new property?
                MyView()
            }
        }
    }
}

class MyObject: ObservableObject {
  @Published var items = [MyItem]()
}

@NicholasBellucci
Copy link
Owner

I think you are correct with this approach as the Tab shouldn't need to ever directly change the value. PR looks good to me so I will go ahead and merge.

@NicholasBellucci NicholasBellucci merged commit c8aebfe into NicholasBellucci:master Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants