Skip to content

Instantly share code, notes, and snippets.

@pgpt10
Last active June 10, 2018 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgpt10/b602e13611ca2acc3b0bd1d65f3b2c31 to your computer and use it in GitHub Desktop.
Save pgpt10/b602e13611ca2acc3b0bd1d65f3b2c31 to your computer and use it in GitHub Desktop.
class Person: NSObject, NSCopying
{
var name: String?
var address: Address?
init(_ name: String?, _ address: Address?)
{
self.name = name
self.address = address
}
func copy(with zone: NSZone? = nil) -> Any
{
let person = Person.init(self.name, self.address)
return person
}
}
var a1 = Address("Mumbai")
var p1 = Person("John", a1)
var p2 = p1.copy() as! Person //This will create a Deep Copy of p1
print("Name: \(p1.name), City: \(p1.address?.city)") //Name: John, City: Mumbai
print("Name: \(p2.name), City: \(p2.address?.city)") //Name: John, City: Mumbai
p2.name = "Stefen"
p2.address?.city = "Bangalore"
print("Name: \(p1.name), City: \(p1.address?.city)") //Name: John, City: Bangalore
print("Name: \(p2.name), City: \(p2.address?.city)") //Name: Stefan, City: Bangalore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment