Ajay Kumar
Ajay Kumar

Reputation: 1827

Right way to implement deep copy for object which contains nested objects in Swift?

I am trying to get deep copy of an class object which contains nested object in swift, Can anyone help ?

Code which I tried

Here is my class:

class ZLFilters: NSObject, NSCopying {

var filterChoices   : [ZLFilterChoice]?


required init(_ map: Map){

}

init(filterChoices : [ZLFilterChoice]) {

    self.filterChoices = filterChoices
}

func copyWithZone(zone: NSZone) -> AnyObject {

    let copy = ZLFilters(filterChoices: filterChoices!)
    return copy
  }
}

But the problem is filterChoices is itself an object, so again internally it does not do deep copy.

Upvotes: 2

Views: 575

Answers (1)

Wain
Wain

Reputation: 119031

Assuming your filter choice also conforms:

let copy = ZLFilters(filterChoices: filterChoices!.copyWithZone(zone))

Upvotes: 2

Related Questions