0

enter image description here

I want to save different struct in a array. And i want to update the array when any of the struct value changed.

My structs are -


struct A{
    var name: String
    var village: String
}
struct B{
    var name: String
    var village: String
}
struct C{
    var name: String
    var village: String
}
struct D{
    var name: String
    var village: String
}
struct E{
    var name: String
    var village: String
}

class ViewController: UIViewController {

    var dataArray = [A.self, B.self, C.self, D.self, E.self] as [Any]
    
    override func viewDidLoad() {
        super.viewDidLoad()
  
    }

    private func changeStructValue(){
        
        //update struct D Value
        if let nameOne = textFieldOne.text{
        let a = A(name: nameOne, village: “abcd”)
        dataArray[0] = a
        }
        
        //update struct D Value
        if let nameFour = textfieldFour.text{
            let d = D(name: nameFour, village: “efgh”)
            dataArray[3] = d
        }
    }

Can I do like this? Or I am doing in wrong way?

And also I want to delete the struct which is nil from the array -

 private func submitButton(){
     
     //Remove empty struct
     dataArray = dataArray.filter { $0 != nil }
 }
}

can I try like this?

9
  • 3
    Why do you have 5 structurally identical struct definitions? Commented Sep 30, 2020 at 18:53
  • i have 5 textfields present in 5 different cells of a tableview. When I am entering the value in the textfield, I want to update the struct . So I have taken 5 different struct . Commented Sep 30, 2020 at 19:01
  • 2
    @TapanRaut why not use one struct and different instances of the same struct? Commented Sep 30, 2020 at 19:03
  • @gcharita thanks for your response. Actually i am unable to do this by using the same struct. can yo suggest how to do this, so that the array will be updated according to the textfield present in tableview. Commented Sep 30, 2020 at 19:08
  • @TapanRaut your TableView has static or dynamic cells? Will be helpful if you add more of your code in the question. Commented Sep 30, 2020 at 19:16

1 Answer 1

3

I think you can simplify your code by using a protocol with the shared properties and methods:

protocol MyProtocol {
    var name: String { get set }
    var village: String { get set }
}

struct A: MyProtocol {
    var name: String
    var village: String
}
struct B: MyProtocol {
    var name: String
    var village: String
}
struct C: MyProtocol{
    var name: String
    var village: String
}
struct D: MyProtocol{
    var name: String
    var village: String
}
struct E: MyProtocol{
    var name: String
    var village: String
}

Then on the view controller:

var dataArray: [MyProtocol] = []
Sign up to request clarification or add additional context in comments.

1 Comment

There's an underlying issue, which this fixes but doesn't remove: why are there 5 structurally identical structs in the first place?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.