1

I have an instance of Addon class (inherited from NSManagedObject) foundSampleAddon. I want to create a copy of this instance of class and mutate its properties. I am trying to copy this instance using the following code but I am not sure how to achieve it as I am getting an exception

-[Addon copyWithZone:]: unrecognized selector sent to instance 0x7f9d1d805000

I dont know how to create copy of this instance of class using the copyWithZone: function. I have posted the code below where I am trying to copy foundSampleAddon into a new identity, sampleAddonToAdd and then changing its property productAddonPrice.

if let sampleAddonToAdd = foundSampleAddon.copy() as? Addon {
        if addonCategoriesSent![sentIndexPath!.section].replacePreviousBasePrice == 1 {
             sampleAddonToAdd.productAddonPrice = NSNumber(int: 0)
        }
        addonsToAddBackToProduct.append([keyAnAddon: sampleAddonToAdd, keyAddonCount: 1])
}
Ishaan Sejwal
  • 660
  • 9
  • 29

2 Answers2

1

NSManagedObject does not conform to the NSCopying protocol. If you want to create a new record with the same data, just insert a new object and assign the values from the first object to the second object.

As said here

Community
  • 1
  • 1
Tancrede Chazallet
  • 7,035
  • 6
  • 41
  • 62
0

You have to create a second NSManagedObject and copy the properties.

let sampleAddonToAdd = NSEntityDescription.insertNewObjectForEntityForName("Addon", inManagedObjectContext: self.managedObjectContext) as! Addon
sampleAddonToAdd.productAddonPrice = foundSampleAddon.productAddonPrice.copy() // copy properties

Then modify the properties you want to.

orkoden
  • 18,946
  • 4
  • 59
  • 50