Last active
June 10, 2018 13:02
-
-
Save pgpt10/b602e13611ca2acc3b0bd1d65f3b2c31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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