Jan Jan - 2 months ago 13
iOS Question

How to duplicate a custom object?

I need to duplicate a custom object. Is is possible without copying each and every field manually?

var test1 = CustomerInfo()
var test2 = CustomerInfo()
test1 = test2
test1.customername = "First"
test2.customername = "Second"

print(test1.customername) /* Getting "Second" , actually i want to get "First" */


Please suggest me any possible solution?

Answer Source

You can use NSCopying class for copying objects.

Refer to this article with example, https://www.hackingwithswift.com/example-code/system/how-to-copy-objects-in-swift-using-copy