SWIFTUI - update an Array/property Element gives error (had to replace the Whole structure)

SWIFTUI - update an Array/property Element gives error (had to replace the Whole structure), not sure my syntax is correct

and Gives Syntax error.. Cannot assign value of type 'Double' to type 'Binding<Subject>' , snippet of code below

BTW, I was able to use AllObjects.Replace (but it is process intensive & hanges ) .. replaceSubrange(index...index, with: [TestObjects(.. latitude:Double(-999.0) //... etc )]

Perhaps My syntax has issue .. please help

//code snippets //declaration

@State private var AllObjects:[TestObjects] = []

func TestObjUpdateLoc(index:Int32) { AllObjects.TestObjects[index].latitude = Double(-999.0)

**//error ..Cannot assign value of type 'Double' to type 'Binding<Subject>' **

}

// TestObjects struct TestObjects: Codable, Hashable { let seq_id:Int32 let latitude:Double let longitude:Double // Many more... }

Answered by Claude31 in 819608022

When you post code, please use code formatter, so that we get a more readable code.

I've completed to check your code and found many issues (the one you report is a consequence of them):

    1. what does this mean ?
AllObjects.TestObjects

AllObjects (it should be written allObjects as it is an instance, not a class or structure definition) does not contain TestObjects property

So replace with

        allObjects[index].latitude = Double(-999.0)
  • 2  index is declared as Int32. Why so ? If it is an index in the array, it should be Int. Or do you want to pass a seq_id ? If so, you would have to change the logic of the func.

  • 3 latitude is a constant. You cannot change it once assigned. Note that a latitude of -999 is meaningless.

With all those errors corrected, it works, as you can test yourself.

If that's OK, don't forget to close the thread by marking the answer as correct.

struct ContentView: View {
    @State private var allObjects:[TestObjects] = [TestObjects(seq_id: 1, latitude: 45, longitude: 2)]
    
    func TestObjUpdateLoc(index:Int) {  // CHANGED to Int
        if index >= 0 && index < allObjects.count { // Important to avoid crashes}
            allObjects[index].latitude = Double(-999.0)
        }
    //error ..Cannot assign value of type 'Double' to type 'Binding<Subject>' **
    }
    var body: some View {
        Text("Hello \(allObjects[0].latitude)")
        
        Button(action: {
            TestObjUpdateLoc(index: 0)
        }) {
            Text("Test")
        }
    }
}

struct TestObjects: Codable, Hashable {
    let seq_id:Int32
    var latitude:Double     // Changed to var
    var longitude:Double    // Changed to var
    // Many more...
}

When you post code, please use code formatter, so that we get a more readable code.

I've completed to check your code and found many issues (the one you report is a consequence of them):

    1. what does this mean ?
AllObjects.TestObjects

AllObjects (it should be written allObjects as it is an instance, not a class or structure definition) does not contain TestObjects property

So replace with

        allObjects[index].latitude = Double(-999.0)
  • 2  index is declared as Int32. Why so ? If it is an index in the array, it should be Int. Or do you want to pass a seq_id ? If so, you would have to change the logic of the func.

  • 3 latitude is a constant. You cannot change it once assigned. Note that a latitude of -999 is meaningless.

With all those errors corrected, it works, as you can test yourself.

If that's OK, don't forget to close the thread by marking the answer as correct.

struct ContentView: View {
    @State private var allObjects:[TestObjects] = [TestObjects(seq_id: 1, latitude: 45, longitude: 2)]
    
    func TestObjUpdateLoc(index:Int) {  // CHANGED to Int
        if index >= 0 && index < allObjects.count { // Important to avoid crashes}
            allObjects[index].latitude = Double(-999.0)
        }
    //error ..Cannot assign value of type 'Double' to type 'Binding<Subject>' **
    }
    var body: some View {
        Text("Hello \(allObjects[0].latitude)")
        
        Button(action: {
            TestObjUpdateLoc(index: 0)
        }) {
            Text("Test")
        }
    }
}

struct TestObjects: Codable, Hashable {
    let seq_id:Int32
    var latitude:Double     // Changed to var
    var longitude:Double    // Changed to var
    // Many more...
}
Accepted Answer

Sorry, this the very first post i did on this forum & I did not realize the formatting issue, sorry thankyou verymuch for your answer:

  1. in the struct TestObjects latitude is wrong as you pointed out THANKS ,

  2. the example where it gives error :

allObjects[index].latitude = Double(-999.0) is just a dummy value I assigning to indicate the row is invalid to be assigned later

Once again THANKYOU Claud

SWIFTUI - update an Array/property Element gives error (had to replace the Whole structure)
 
 
Q