2

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.

Ajay Kumar
  • 1,807
  • 18
  • 27

1 Answers1

2

Assuming your filter choice also conforms:

let copy = ZLFilters(filterChoices: filterChoices!.copyWithZone(zone))
Wain
  • 118,658
  • 15
  • 128
  • 151
  • 1
    var filterChoices : [ZLFilterChoice]?, As filterchoices is an array of ZLFilterChoice, so I can't call copyWithZone(zone) on filterChoices(array) object. – Ajay Kumar Dec 26 '16 at 11:00