Xcode should generate Swift designated initializers for NSManagedObject subclasses

Number:rdar://21098460 Date Originated:25 May 2015
Status:Open Resolved:
Product:Xcode Product Version:6.3.2 (6D2105)
Classification:Serious bug Reproducible:Always
Because of Swift's strict initialization flow and @NSManaged, subclasses of NSManagedObject generated by Xcode bypass Swift's initialization rules, allowing you to instantiate an object that **has not** been fully initialized. Thus, accessing non-optional properties on NSManagedObject subclass will crash at runtime.

Steps to Reproduce:
1. Create an entity in Core Data with a few non-optional attributes
2. Generate NSManagedObject subclass in Swift
3. Instantiate the class
4. Access properties
5. Crash at runtime

Expected Results:
Consider this example entity. This is what Xcode should generate.

It should generate a full designated initializer, with any specified default values from the core data model.

class TestEntity: NSManagedObject {

    @NSManaged var myInt: Int32
    @NSManaged var myDouble: Double
    @NSManaged var myFloat: Float
    @NSManaged var myString: String?
    @NSManaged var myDate: NSDate?

    init(context: NSManagedObjectContext,
         myInt: Int32,
         myDouble: Double,
         myFloat: Float,
         myString: String? = "Default value for my string",
         myDate: NSDate? = nil) {
            let entity = NSEntityDescription.entityForName("TestEntity", inManagedObjectContext: context)!
            super.init(entity: entity, insertIntoManagedObjectContext: context)

            self.myInt = myInt
            self.myDouble = myDouble
            self.myFloat = myFloat
            self.myString = myString
            self.myDate = myDate


Actual Results:
Xcode does not generate a designated initializer in Swift.

class TestEntity: NSManagedObject {

    @NSManaged var myInt: Int32
    @NSManaged var myDouble: Double
    @NSManaged var myFloat: Float
    @NSManaged var myString: String
    @NSManaged var myDate: NSDate

6.3.2 (6D2105)





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!