swiftcore-datakeypaths

CoreData update relationship method


I have two entities - Quote and Customer (one customer can have many quotes)

Say I have a managed object called quote but I want to update the customer address. Is there a keypath that gets me to the customer address? As in:

quote!.setValue(address, forKeyPath: “some_amazing_keypath")

Solution

  • Assuming the Customer entity has an attribute “address” which holds the address, the keypath should just be “customer.address”:

    quote!.setValue(address, forKeyPath: “customer.address")
    

    But life would be much easier if you specify a subclass for the entity: you can then just use:

    quote!.customer.address = address