O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Copying reference types

Two variables can point to the same object, so changing one variable changes the other too. Having lots of objects point to the same data can be useful in some circumstances, but mostly we will want to modify copies so that modifying one object doesn't have an effect on the others. The foundation framework provides us the means to copy reference types. To make it work, we need to do the following:

  • Our class should be of the NSObject type
  • Our class should conform to the NSCopying protocol (which is not mandatory, but makes our intent clear for our API user)
  • Our class should implement the copy(with: NSZone) method
  • To copy the object, we will need to call the copy() method on the object

Here's an example of a Manager ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required