Reputation: 1827
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
Reputation: 119031
Assuming your filter choice also conforms:
let copy = ZLFilters(filterChoices: filterChoices!.copyWithZone(zone))
Upvotes: 2