I have a class AEUser which is a subclass of NSManagedObject. That class has a method which creates a new instance of itself. For the default target, the normal route works well:
static func newDefaultUser(moc: NSManagedObjectContext) -> AEUser {
let newUser = NSEntityDescription.insertNewObjectForEntityForName("AEUser", inManagedObjectContext: moc) as! AEUser
newUser.uid = sDefaultUser // some string constant
newUser.name = sDefaultUser // some string constant
return newUser
}
However, when executing a XCTestCase which uses the same function to create a new user, a cast error occurs at runtime. I can suddenly not cast the new object into an AEUser anymore. On the default target, everything worked fine though.
Now interestingly the "longer" variant of the above code works flawlessly for the test case. Now a cast is of course not necessary any more. But the above code should produce the exact same object. Also, when using breakpoints and looking into the actual object assigned to newUser above, it seems like it is a AEUser object as should be.
private static func newDefaultUser(moc: NSManagedObjectContext) -> AEUser {
let entity = NSEntityDescription.entityForName("AEUser", inManagedObjectContext: moc)
let newUser = AEUser(entity: entity!, insertIntoManagedObjectContext: moc)
newUser.uid = sDefaultUser // some string constant
newUser.name = sDefaultUser // some string constant
return newUser
}
Can somebody explain why the above code does not work when using XCTestCase / a separate test target, specifically why the runtime cast fails in spite of the above code seemingly producing an object of the proper type just like the second variant?
(Running Xcode 6.3.2 and the iOS 8.3 SDK.)