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

Enforce private properties in SwiftUI View's #873

Open
sindresorhus opened this issue Mar 1, 2021 · 2 comments
Open

Enforce private properties in SwiftUI View's #873

sindresorhus opened this issue Mar 1, 2021 · 2 comments

Comments

@sindresorhus
Copy link

Apple recommends that state properties in SwiftUI View's are private:

You should only access a state property from inside the view’s body, or from methods called by it. For this reason, declare your state properties as private, to prevent clients of your view from accessing it. - https://developer.apple.com/documentation/swiftui/state

All of these should be marked as private:

struct ContentView: View {
	@State var foo = ""
}
struct ContentView: View {
	@ObservedObject var foo = Foo()
}
struct ContentView: View {
	@EnvironmentObject var foo
}
struct ContentView: View {
	@StateObject var foo = Foo()
}
struct ContentView: View {
	@Environment(\.colorScheme) var colorScheme
}
@nicklockwood
Copy link
Owner

This seems doable. It would have to be an opt-in rule since it has potential to break existing code (even if the code in question deserves to be broken).

@rudyrichter
Copy link

rudyrichter commented Jun 17, 2022

@nicklockwood this would be great, thoughts on giving it private and internal as options?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants