Swift ManagedObject fails to cast to subclass in test target

Originator:jesse.squires.developer
Number:rdar://19368054 Date Originated:02-Jan-2015
Status:Open Resolved:
Product:Developer Tools Product Version:6.1.1 (6A2008a)
Classification:Serious Bug Reproducible:Always
 
Summary:
Calling NSEntityDescription.insertNewObjectForEntityForName(entityName: context:) to instantiate a custom managedObject subclass fails to cast.

For example:
let person = NSEntityDescription.insertNewObjectForEntityForName("Person", inManagedObjectContext: context) as? Person

This fails ONLY if the class is internal and is added to the test target compile sources. (So that the test target recognizes the class)

Current workarounds:

(1) If the class is public and NOT add to the test target, but instead imported (import AppNameTarget), then casting succeeds.

(2) Implement the following convenience initializer:

class Person: NSManagedObject {

    convenience init(context: NSManagedObjectContext) {
        let entityDescription = NSEntityDescription.entityForName("Person", inManagedObjectContext: context)!
        self.init(entity: entityDescription, insertIntoManagedObjectContext: context)
    }

    override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) {
        super.init(entity: entity, insertIntoManagedObjectContext: context)
    }
}

Steps to Reproduce:
See the attached demo project.

1. Build and run the unit tests
2. Note the passing test, and the failing test

Expected Results:
The NSManagedObject subclass should be successfully casted from AnyObject to its proper class upon insertion into Core Data.

All unit tests should pass.

Actual Results:
The NSManagedObject subclass fails to cast from AnyObject to its proper class upon insertion into Core Data.

Unit test fails.

Version:
6.1.1 (6A2008a)

Attachments:
https://github.com/jessesquires/rdar-19368054

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!