UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How can I edit data in a detail view so that it gets reflected in the master view?

Forums > SwiftUI

Hi all, quite new to SwiftUi, thanks to Paul I make some progress. Having finished Project 10 of "SwiftUI in 100 days" I tried to write a first little app on my own reading from and writing to a little API I wrote to see the data from my photovoltaic system. But I came across a problem I can't solve on my own (well, I did solve it, sort of, but my feeling is, that there must be a better solution).

It's all about master and detail view - and editing data in the detail view. I wrote a most simple (and totally useless) app to demonstrate my problem. When I switch to the detail view - how can I pass my variable in so that any change I make there gets reflected in the main view? I just want to edit the data from the respective row.

What I tried is this, but there must be a better solution than to look for the UUID of the row?

struct ContentView: View {
    class Test: ObservableObject {
        struct Row {
            var id: UUID
            var name: String
            var nummer: Int

            init(name: String, nummer: Int) {
                self.name = name
                self.nummer = nummer
                self.id = UUID()
        @Published var title = "Test"
        @Published var rows = [Row(name: "Item 1", nummer: 1), Row(name: "Item 2",nummer: 2), Row(name: "Item 3", nummer: 3), Row(name: "Item 4",nummer: 4)]

    struct detailView: View {

        @ObservedObject var test: Test
        var itemID: UUID

        var body: some View {
            Form {
                TextField(test.rows[getItemByID(itemID: itemID)].name, text: $test.rows[getItemByID(itemID: itemID)].name)
                Text("\(test.rows[getItemByID(itemID: itemID)].nummer)" )

        func getItemByID(itemID: UUID) -> Int {
            return test.rows.firstIndex(where: {$0.id == itemID}) ?? 0

    @StateObject var test = Test()

    var body: some View {
        NavigationView {

            List {
                Section("Title") {
                Section("Title") {
                    ForEach(test.rows, id: \.nummer) { row in
                        NavigationLink {
                            detailView(test: test, itemID: row.id)
                        } label: {



I don't think you're far off. The biggest issue I see is your data model looks like you put it together in a way that "solves" some of your issues with your View design.

If you start with a cleaner data model, I think you'll find a clearer path to your solution.

I'd probably start with a class as a container for your editable data, constructed as an @ObservableObject, and have it expose data struct(s) or even just properties directly.


There was a similar discussion about this topic over at this thread. See -> Modify array in an observable object

Also, a similar article here: Nested Observable Objects

I remember reading on StackOverflow some code creating ObservableArray. This would publish any time a value INSIDE an array element changes. Probably closer to what you'd like to see.


Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.