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

Copy constructors

The first solution is to provide a new init method that copies the current instance. This approach is called a copy constructor. Let's add our new init method and leverage it:

init(products: [FunctionalProduct], lastModified: Date) {     self.products = products     self.lastModified = lastModified } init(productTracker: FunctionalProductTracker,     products: [FunctionalProduct]? = nil,      lastModified: Date? = nil) {     self.products = products ?? productTracker.products     self.lastModified = lastModified ?? productTracker.lastModified } 

We added the default init as well because by adding a new init method to our struct, we lost the benefit of automatic init generation. We also need to change our addNewProduct to accommodate these ...

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