Setting an NSManagedObject relationship in Swift Setting an NSManagedObject relationship in Swift ios ios

Setting an NSManagedObject relationship in Swift


As of Xcode 7 and Swift 2.0 (see release note #17583057), you are able to just add the following definitions to the generated extension file:

extension PersonModel {    // This is what got generated by core data    @NSManaged var name: String?    @NSManaged var hairColor: NSNumber?    @NSManaged var parents: NSSet?    // This is what I manually added    @NSManaged func addParentsObject(value: ParentModel)    @NSManaged func removeParentsObject(value: ParentModel)    @NSManaged func addParents(value: Set<ParentModel>)    @NSManaged func removeParents(value: Set<ParentModel>)}

This works because

The NSManaged attribute can be used with methods as well as properties, for access to Core Data’s automatically generated Key-Value-Coding-compliant to-many accessors.

Adding this definition will allow you to add items to your collections. Not sure why these aren't just generated automatically...


Yeah that's not going to work anymore, Swift cannot generate accessors at runtime in this way, it would break the type system.

What you have to do is use the key paths:

var manyRelation = myObject.valueForKeyPath("subObjects") as NSMutableSetmanyRelation.addObject(subObject)/* (Not tested) */


Core Data in Objective C automatically creates setter methods (1):

By default, Core Data dynamically creates efficient public and primitive get and set accessor methods for modeled properties (attributes and relationships) of managed object classes. This includes the key-value coding mutable proxy methods such as addObject: and removes:, as detailed in the documentation for mutableSetValueForKey:—managed objects are effectively mutable proxies for all their to-many relationships.

As things currently stand with Swift in Xcode6-Beta2, you'd have to implement those accessors yourself. For example if you have an unordered to-many relationship, from Way to Node, you'd implement addNodesObject like this:

class Way : NSManagedObject {    @NSManaged var nodes : NSSet    func addNodesObject(value: Node) {        self.mutableSetValueForKey("nodes").addObject(value)    }}

Key here is that you'd have to use mutableSetValueForKey / mutableOrderedSetValueForKey / mutableArrayValueForKey. On these sets / arrays, you can call addObject and they'll be stored on the next flush.