salma209
salma209

Reputation: 189

want to update data after editing

i want to update data after editing. for that i need the previous data. but in my case after editing, the variable that hold the previous value are replaced by the present value. how can i hold the previous value??

 if itemToEdit == nil{
        item = TestDataModel()

    else{
        item = itemToEdit
        selectedItemToEdit = itemToEdit
      //  print(selectedItemToEdit?.title)
         print(item.title)
      //  print(itemToEdit?.title)
    }

    if let title = titleField.text {
        item.title = title
        print(item.title)
    }
  //  print(selectedItemToEdit.title!)
    print(itemToEdit?.title!)

    if let price = pricefield.text {
        item.price = (price as NSString).doubleValue
        print(item.price)
    }

    if let details = detailsField.text {
        item.details = details
        print(item.details)
    }

itemToEdit, is that which i want to edit/ update. that's why i stored it in selectedItemToEdit. in item, i have stored the data after editing. i need both the previous value and the present value that i entered in textfield. how can i get that one.please any one help....

Upvotes: 0

Views: 63

Answers (2)

salma209
salma209

Reputation: 189

yeah i solved the problem: i just declared a variable of string type as bellow:

  var selectedItemToEdit: string!
  selectedItemToEdit = itemToEdit.title

i passed that string type variable and the edited variable to the save function.

Upvotes: 0

Phillip Mills
Phillip Mills

Reputation: 31016

What you're doing when you try to save previous information is simply creating a new variable that points to the same object. When the object changes, it doesn't matter what variable you use to examine it, you will see the changes in that one object.

Here's a playground sample that should give you an idea of the difference between having two objects vs. having two pointers to the same object.

class MyData {
    var title = "Default"

    init() {
    }

    init(source: MyData)
    {
        title = source.title
    }
}

var d1 = MyData()
var d2 = d1
var d3 = MyData(source: d1)
d1.title = "changed"

print(d2.title)
print(d3.title)

Upvotes: 1

Related Questions