Skip to content

Instantly share code, notes, and snippets.

@pgpt10 pgpt10/DeepShallow4.swift
Last active Jun 10, 2018

What would you like to do?
class Person: NSObject, NSCopying
var name: String?
var address: Address?
init(_ name: String?, _ address: Address?)
{ = name
self.address = address
func copy(with zone: NSZone? = nil) -> Any
let person = Person.init(, 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: \(, City: \(p1.address?.city)") //Name: John, City: Mumbai
print("Name: \(, City: \(p2.address?.city)") //Name: John, City: Mumbai = "Stefen"
p2.address?.city = "Bangalore"
print("Name: \(, City: \(p1.address?.city)") //Name: John, City: Bangalore
print("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
You can’t perform that action at this time.