1

please help.

I have 3 NSManaged objects.

say Employee, Payslip, PayItem

Each Employee can have multiple Payslips, each Payslip can have multiple PayItems.

so the relationship is Employee <->> Payslip <<- PayItem

They are all set up as NSManagedOjects.

Then lets say I have 3 instances of each: (imagine I'm initialising each by adding this to the NSManagedObject class:

  convenience init(context: NSManagedObjectContext)
    {
    let entity = NSEntityDescription.entity(forEntityName: <entity>, in: context)!
    self.init(entity: entity, insertInto: context)'
    }

Then I can declare.

 var employee = Employee(context: context)
        var payslip = Payslip(context: context)
        var payItem = PayItem(context: context) 

I can then:

employee.addToPayslip(payslip) //Using the function created for me by default.

But if I try:

payslip.payItem = payItem

I always get the error:

Failed to call designated initializer on NSManagedObject class 'PayItem'

To summarise, I'm trying to link Employee to a payslip, that is one to many, then a payslip to a payitem, that is one to many. Why am I having such a tough time?

Amanpreet
  • 1,301
  • 3
  • 12
  • 29
LateNate
  • 763
  • 9
  • 22
  • In my knowledge Create a 3 class Employee , PaySlip and Payitem . In Employee class create an Array of paySlip property . In Payslip class create an Array of payItem property. just a model .I didnt used NSManagedObject. – Subin K Kuriakose Dec 27 '16 at 11:58
  • Thanks for the suggestion. I could indeed use arrays of classes. But in this instance. I specifically want to use core data and NSManagedObjects – LateNate Dec 27 '16 at 12:20
  • The code looks fine. I suspect there is something fishy in your model. – shallowThought Dec 27 '16 at 12:34
  • Me to. I've scoured my model and i just cant come up with the answer. Payslip can join with employee fine, payItem cant join to payslip. Its driving me crazy. Literally weeks i've spent on this. – LateNate Dec 27 '16 at 19:22
  • The error suggests that somewhere in your code you are calling the plain `init()` method, rather than the convenience initialiser you have created. This might be in declaring a variable with a default value you never use (as in [this question](http://stackoverflow.com/a/33307824/3985749)). If you can't track it down, try overriding the plain `init`, put a breakpoint in it, and then check the stack trace. – pbasdf Dec 27 '16 at 22:36
  • Thanks @pbasdf!!! You were absolutely right. I had in instance of PayItem() being returned from a return I had completely forgotten about and didnt realise was being called. That helped a lot. Weeks of frustration resolved. – LateNate Dec 28 '16 at 11:33

1 Answers1

0

So it turned out I had a function that was returning an uninitialised version of PayItem. It took a while to track down, but zapping this resolved my problem.

LateNate
  • 763
  • 9
  • 22