tutorial, swiftui,

@State to @ObjectBinding Modifier

Jules Lee Jules Lee Follow Oct 01, 2019 · 1 min read
@State to @ObjectBinding Modifier
Share this

Notice how Scott changed from @State to @ObjectBinding. He started discussing BindableObject at 5:17. That’s because you can only use @State with local properties in the struct view. Let’s see his code one more time.

import Combine

class ListDataSource: BindableObject {
	var willChange = PassthroughSubject<Void, Never> ()
	var rowModels = [CustomRowModel] () {
		willSet {

In this tutorial, that’s all the code we need. Just kidding. There’s one more:

struct ContentView: View {
	@ObjectBinding var datasource = ListDataSource()
	// to access the rowModels array

Notice here that he used @ObjectBinding instead of @State. If you want a hold of the state of a class, then you must use @ObjectBinding instead and that class should inherit from BindableObject protocol, which requires that you have a PassthroughSubject. In order to use PassthroughSubject, import Combine first. You can substitude willChange to didChange. Heck, it’s just a variable name. But if you do, also change the willSet to didSet. willChange.send() will notify the subscribed view, which in this case is ContentView, of any changes done to rowModels array.

Jules Lee
Written by Jules Lee
Hi, I am Jules, the author of FlutterGeek. Demystifying complex instructions from the internet.