Core Data - Create NSManagedObject Subclass

I created a test app called Template9b1. The first step I've taken is to use a Template Master / Detail with Core Data. I modified the Entity (called Event) with 4 properties instead of the default "timestamp". I used Create NSManagedObject Subclass, and it created the 2 files as it should.


// Event+CoreDataProperties.swift


import Foundation

import CoreData

extension Event {

@NSManaged var theDate: NSDate?

@NSManaged var name: String?

@NSManaged var favorite: String?

@NSManaged var status: NSNumber?

}


// Event.swift

import Foundation

import CoreData

@objc(Event)

class Event: NSManagedObject {

// Insert code here to add functionality to your managed object subclass

}


When I run it (simulator), I get an error


2015-06-16 23:34:48.842 Template9b1[50760:1014766] CoreData: warning: Unable to load class named 'Template9b1.Event' for entity 'Event'. Class not found, using default NSManagedObject instead.


In the past, to get rid of this type of error, I would put the name of the app "Template9b1" in the module for the inspector of the .xcdatamodeld. But this doesn't seem to get rid of this problem. Also when trying to use it crashes with the following error.


let thisEvent = NSEntityDescription.insertNewObjectForEntityForName(entity.name!, inManagedObjectContext: context) as! Event


Could not cast value of type 'NSManagedObject_Event_' (0x7fa78140b190) to 'Template9b1.Event' (0x10c10b820).


I'm guessing that either the settings for the .xcdatamodeld should be different than they were before, or Event.swift needs something (if it does need something, could I get a lead on what?)

Accepted Reply

It appears you now have to remove @objc(MySubClass) from your NSManagedObject subclass and it should work

Replies

There are a few possibilities:

1. You created that Event outside of the Template9b1 package, so it's not actually Template9b1.Event.

2. When you created the files, they didn't get added to the build phases (and thus aren't being built).


Isn't there an explicit package statement in Swift that you can use?

It appears you now have to remove @objc(MySubClass) from your NSManagedObject subclass and it should work

Yes, removing the @objc(MySubClass) from the Event.swift cleaned up all errors and now it works as it should.

I get this error at compile time: Entity1.Type does not have the member title

I use XCode 7 latest beta (7A121l)


Entity1.swift and Entity1+CoreDataProperties.swift have been created, both have the correct target member ship.


Entity1.swift:

import Foundation

import CoreData

@objc(Entity1)

class Entity1: NSManagedObject {

// Insert code here to add functionality to your managed object subclass

}


Entity1+CoreDataProperties.swift:

import Foundation

import CoreData

extension Entity1{

@NSManaged var title: String?

...

}


Removing @objc(Entity1) does not help

I am seeing a different issue with this which is that there are no methods generated to modify relationships.


For example, Person is many-to-many with Location, person -> locations is represented as a NSMutableOrderedSet.


XCode 7b3 is generating this as person <-> locations, where locations is a NSOrderedSet? — and offers no accompanying addLocationObject or similar.


So I don't know how to add locations to a person without forcibly overriding the stuff that Xcode generated...


Anybody have any thoughts on this? Am I missing something obvious?


Thanks,

Dave