Xcode 8 generates broken NSManagedObject subclasses for iOS 10

后端 未结 22 836
栀梦
栀梦 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 12:08

    In my particular case, I had added CoreData to an existing project which already had a Model class by the same name as my entity. Deleting the old Model class fixed the problem, as its type was colliding with the new CoreData Entity class.

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

    I am running the latest Xcode 8.1 beta (8T47) build.

    According to the error log (see below), two copies of the autogenerated files are created. One copy is put in your Xcode project folder (the one visible in your directory on the left hand side toolbar of Xcode), and a second copy is created in your DerivedData folder for your project:

    /Users/<your name>/Library/Developer/Xcode/DerivedData/<your app>-axhtnodotznxnrenefflktnxfeal/Build/Intermediates/<your app>.build/Debug-iphonesimulator/<your app>.build/DerivedSources/CoreDataGenerated/<your app>/<class name>+CoreDataProperties.swift
    /Users/<your name>/Library/Developer/Xcode/DerivedData/<your app>-axhtnodotznxnrenefflktnxfeal/Build/Intermediates/<your app>.build/Debug-iphonesimulator/<your app>.build/DerivedSources/CoreDataGenerated/<your app>/<class name>+CoreDataClass.swift
    

    Deleting the copies in your Xcode project directory will fix all issues, but proves pointless as you can no longer edit these files...

    I wouldn't say this is a solution, rather just a half-assed way to get the project to build. Hopefully this bug will be addressed soon.

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

    I believe the reason you encountered those errors after generating the NSManagedObject subclass files may be related to the Codegen option that was selected at the time your data model changed.

    I do not think this is a bug. At least not in Xcode 8.1 (8B62).

    I encountered a similar issue and fixed it by changing the Codegen option to "Manual/None" and leaving the Module option as "Global Namespace". I then generated my NSManagedObject subclass files. However, depending on your situation, you may not even need to generate the NSManagedObject subclasses.

    Below are more details on the entity Codegen options based my review of Apple's documentation, the Apple developer's forum and testing with my own project.

    Option 1: "Class Definition"

    • Think of this as the "plug and play" option

    • Use the model editor to create the entities and associated attributes that you want Core Data to manage.

    • Do not use the NSMangagedObject subclass generator. The required files are automatically generated for you by Xcode (however they do not show up in your project navigator).

    • If you do generate the NSManagedObject files, Apple tells you that these files should not be edited in the header comments of said files. If you do need to edit these files, then this is good sign that you need to use another Codegen option.

    • Leave the default Class options for your entity (Module = Global Namespace and Codegen = Class Definition)

    • If you need to override methods of the NSManagedObject, you can create an extension using the Name under the Class option.

      // Optional extension in Entity.swift extension Entity { // Override NSManagedObject methods }

    Options 2: "Manual/None"

    • Similar to Option 1, except that you can see and edit the NSManagedObject subclass files.

    • Use the model editor to create the entities and associated attributes that you want Core Data to manage.

    • Before using the NSManagedObject subclass generator, set the Codgen option to "Manual/None" for the entity. The Module option should be Global Namespace.

    • After you add/remove/update an attribute, you have two choices: (1) Manually update the NSManagedObject files that were generated for you OR (2) Use the NSManagedObject subclass generator again (this will only update the Entity+CoreDataProperties.swift file).

    • Again, if you need to override and methods from NSManagedObject, you can create an extension. The extension should be created in the Entity+CoreDataClass.swift file and not the Entity+CoreDataProperties.swift file (this file could be updated due to model editor changes).

      // Optional extension in Entity+CoreDataClass.swift extension Entity { // Override NSManagedObject methods }

    Option 3: "Category/Extension"

    • Use this option if you have custom properties that do not need to be managed by Core Data.

    • Use the model editor to create the entities and associated attributes that you want Core Data to manage.

    • Ensure the Module option is set to Current Product Module and the Codegen option is set to "Category/Extension".

    • Create your own NSManagedObject subclass with the properties that you will self manage.

    • Do not use the NSMangagedObject subclass generator. The required files are automatically generated for you by Xcode (however they do not show up in your project navigator).

      // Subclass NSManagedObject in Entity.swift class Entity: NSManagedObject { // Self managed properties }

      // Optional extension in Entity.swift extension Entity { // Override NSManagedObject methods }

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

    I solved it like an example given below

    Create CurrentcyPair.xcdatamodeld

    select CurrentcyPair.xcdatamodeld and select Default -> Entity (CurrencyPair) , rename the class as (.ManagedCurrencyPair)

    Select Entity and open Inspector. Set code gen at data model as Manual/None

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