Paweł Madej
Paweł Madej

Reputation: 1279

Swift - update object in place

If I want to update existing class object is there any easier way than duplicating init to update method? (my Person object has about ten properties which are defined firstly or can be edited at later app usage)

problems listed in example below:

  1. person1 reinitialization does not update reference to a person1 object in a parent-child array - child array does not contain updated object

  2. person2 object behaves as wanted (child array contains updated object) but uses duplicated initializer code in update method to make “edit in place” which looks really bad in terms of DRY

any hints how to do it Swiftly?

import UIKit

class Person {
    var name: String
    var surname: String
    var age: Int?

    init(name: String, surname: String, age: Int? = nil) {
        self.name = name
        self.surname = surname
        self.age = age
    }

    func update(name: String, surname: String, age: Int? = nil) {
        self.name = name
        self.surname = surname
        self.age = age
    }
}
class Parent {
    var child = [Person]()
}

var parent = Parent()

var person1 = Person(name: "John", surname: "Doe")
print(person1.name)
parent.child.append(person1)
person1 = Person(name: "Jack", surname: "Doe")
print(person1.name)

print(parent.child.first!.name)

var person2 = Person(name: "Tom", surname: "Cruise")
print(person2.name)
parent.child.append(person2)
person2.update(name: "Jim", surname: "Cruise")
print(person2.name)

print(parent.child.last!.name)

to make it more clear think about the array of user profiles where you can add a new profile or edit an existing one. and above problem is about editing existing user profile

Upvotes: 0

Views: 1628

Answers (1)

PGDev
PGDev

Reputation: 24341

Here is the explanation for both of your queries,

1. parent child array contains the reference to the person1 object, i.e. person1 and parent child array store the reference to same object.

var person1 = Person(name: "John", surname: "Doe")
parent.child.append(person1)

Now, you're assigning a new object to person1 instead of making changes to the same object, i.e.

person1 = Person(name: "Jack", surname: "Doe")

This won't reflect in the parent child array. It will still contain the reference to the previous Person object.

This is the reason you are getting different values of name in person1 and parent child array

print(person1.name) //Jack
print(parent.child.first!.name) //John

2. Instead of adding a separate method for updating the object - update(name:surname:age:), you can simply update the values individually using the . operator on object's properties, i.e

var person2 = Person(name: "Tom", surname: "Cruise")
parent.child.append(person2)

person2.name = "Jim"

Since in the above code, instead of assigning a new Person object to person2, we're just updating its values.

So, person2 and parent child array's object will still reference the same object and hence will print the same name values.

print(person2.name) //Tom
print(parent.child.last!.name) //Tom

Upvotes: 1

Related Questions