Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

What is the proper way to format a generic subview view when hoping to have it work with both an @State and an @Bindable?

Let's say you have a protocol that can work with both classes and structs but you want to have a uniform UI to make edits.

What is the recommended way to have one view that will take both?

App

import SwiftUI

@main
struct ObservationTesterApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView(existence: Existence())
        }
    }
}

Types

import Foundation


protocol Dateable {
    var timestamp:Date { get set }
}

struct Arrival:Dateable {
    var timestamp:Date
}

@Observable
class Existence:Dateable {
    var timestamp:Date
    
    init(timestamp: Date) {
        self.timestamp = timestamp
    }
}

extension Existence {
    convenience init() {
        self.init(timestamp: Date())
    }
}


ContentView, etc

//
//  ContentView.swift
//  ObservationTester
//
//

import SwiftUI

struct EditDateableView<TimedThing:Dateable>:View {
    @Binding var timed:TimedThing
    
    //note that this currently JUST a date picker
    //but it's possible the protocol would have more
    
    var body:some View {
        DatePicker("Time To Change", selection: $timed.timestamp)
    }
    
}

#Preview {
    @Previewable @State var tt = Arrival(timestamp: Date())
    EditDateableView<Arrival>(timed: $tt)
}

struct ContentView: View {
    @State var arrival = Arrival(timestamp: Date())
    @Bindable var existence:Existence
    
    var body: some View {
        //this work around also not allowed. "self is immutable"
//        let existBinding = Binding<Existence>(get: { existence }, set:  { existence = $0 })
        VStack {
            EditDateableView(timed: $arrival)
            //a Binding cant take a Bindable
            //EditDateableView<Existence>(timed: $existence)
        }
        .padding()
    }
}

#Preview {
    ContentView(existence: Existence())
}
What is the proper way to format a generic subview view when hoping to have it work with both an &#64;State and an &#64;Bindable?
 
 
Q