Xcode 8 generates broken NSManagedObject subclasses for iOS 10

后端 未结 22 837
栀梦
栀梦 2020-11-27 11:13

I updated my iOS app project recently to iOS 10. Now I\'m trying to change the Core Data Model of my app but the new NSManagedObject subclasses which Xcode generates are bro

相关标签:
22条回答
  • 2020-11-27 11:55

    After I renamed the following:

    From: YourEntity+CoreDataClass.swift To: YourEntity.swift

    and

    From: YourEntity+CoreDataProperties.swift To: YourEntityCoreDataProperties.swift

    then it works. It looks like "+" is causing the problem.

    0 讨论(0)
  • 2020-11-27 11:57

    I have a Flights Entity and these settings in the Data Model Inspector worked for me :

    1. Set the Name to nothing(You will see some grayed out text of "NSManagedObject"). The default will be written as "Flights" just remove that.

    2. Nothing in the module."Global Namespace" greyed out is what you will see.

    3. Codegen to Manual/None.

    4. Then Goto Editor -> Create NSManagedObject Subclass to create the swift files

    5. Rename the Flights+CoreDataClass.swift to just Flights.swift

    Here's the link to the Screenshot(For some reason imgur was rejecting it :( ) https://drive.google.com/file/d/0B8a3rG93GTRedkhvbWc5Ujl4Unc/view?usp=sharing

    0 讨论(0)
  • 2020-11-27 12:00

    I finally got mine to work. Here is what I did. (Flights is one of my entities)

    I setup the xcdatamodeld as follows

    And then the entity as

    Then I used Editor -> Create NSManagedObject Subclass

    This creates two files for my flights entity

    Flights+CoreDataProperties.swift

    Flights+CoreDataClass.swift

    I renamed Flights+CoreDataClass.swift to Flights.swift

    Flights.swift is just

    import Foundation
    import CoreData
    
    @objc(Flights)
    public class Flights: NSManagedObject {
    
    }
    

    Flights+CoreDataProperties.swift is

    import Foundation
    import CoreData
    
    
    extension Flights {
    
        @nonobjc public class func fetchRequest() -> NSFetchRequest<Flights> {
            return NSFetchRequest<Flights>(entityName: "Flights");
        }
    
        @NSManaged public var ...
    }
    

    This appears to work for me.I could not get Codegen to work in any other way, even though I tried many of the suggestions that were out there.

    Also this had me scratching my head for a while and I add it as an assist. Don't forget with the new Generics version of the FetchRequest you can do this

    let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Flights")
    
    0 讨论(0)
  • 2020-11-27 12:00

    On Xcode 8.2.1, I made it the way: (thanks for Daniel Chepenko)

    But Xcode 8.2.1 will add a stupid . into the generated files:

    Just delete the . and it will run right.

    0 讨论(0)
  • 2020-11-27 12:02

    In Data Model Inspector select Current Product Module under 'Module' and Manual/None under 'Codegen'. (This allows you to edit subclass files and to regenerate CoreDataProperties.swift when you modify your attributes under the same entity.)

    You can then choose Create NSManagedObjectSubclass under Editor menu. Xcode will create 2 files for you YourEntity+CoreDataClass.swift and YourEntity+CoreDataProperties.swift. Notice if you don't have the latest Xcode (8.2.1), any optional/nonoptional properties that you set in your Model Inspector will not show up correctly. You can edit in YourEntity +CoreDataProperties.swift file.

    0 讨论(0)
  • 2020-11-27 12:04

    I just had a problem where I just added a Person entity in my model to the Core Data template. Then generated the NSManagedObject subclass for for that and it won't compile, giving me a Linker Error. Turns out I am supposed to change the Codegen option to Manual/None to make it work (see bottom-right part of the picture).

    0 讨论(0)
提交回复
热议问题