Core Data Codegen fail in Xcode 8

后端 未结 6 1486
萌比男神i
萌比男神i 2021-02-04 05:01

I have an iOS app containing a Core Data model with 6 entities. The entity Name is set up as follows:

Class Name: Name

相关标签:
6条回答
  • 2021-02-04 05:34

    Regarding Problem 1, setting the Codegen popup in the Inspector for an entity in an .xcdatamodel is supposed to work like this:

    Category/Extension tells Xcode to generate one file, ClassName+CoreDataGeneratedProperties.

    Class Definition tells Xcode to generate two files, the above-named file, plus ClassName+CoreDataClass.

    However, there appears to be a bug, in Xcode 8.2. If, after changing one of these popups, you simply Build (⌘B) or Run (⌘R), your changes will not take effect. For example, if you changed from Class Definition to Category/Extension and even manually deleted the second file, it will reappear. You may even find that the popup reverts back to the original setting.

    In order for changes in these popups to become effective, you must

    1. Save (⌘S) the .xcdatamodel file.
    2. Close all project windows.
    3. Re-open the project.

    During the next build (which may occur automaticallly when the project opens if you have an @IBInspectable in a storyboard), the affected files in Derived Data will be generated or deleted to conform to your new setting.

    UPDATE 2016-12-22: Thank you for the comment, Ashley. I've now written up and submitted this to Apple Bug Reporter: 29789727. Update 2017-02-08: Apple has closed 29789727 as it supposedly duplicates 21205277.

    0 讨论(0)
  • 2021-02-04 05:37

    Looks like Xcode 8.2.1 won't turn off codegen. But you can manually delete codeGenerationType="category" from .xcdatamodel contents file.

    Close Xcode, delete codeGenerationType, delete DerivedData folder and rebuild.

    Will wait for fix in future releases.

    0 讨论(0)
  • 2021-02-04 05:45

    I got completely stuck with Problem 4 and none of the above worked for me. I use code generation. To resolve it I did the following steps:

    1. I opened the .xcdatamodel / content file in a text editor. (In Xcode right-click your .xcdatamodel and choose "Open in external editor"
    2. In the XML file I made sure that each entity has codeGenerationType="class"
    3. Close and reopened Xcode Cleared my derrived data folder
    4. It still refused to do the Code Generation, so I then generated each Entity manually (Editor, Create NSManageObject Subclasses, select all entities)
    5. Build the project. Now it also did the Code Generation and compiler errors appeared saying that the Entity filenames were used twice.
    6. Deleted the Manually created NSManagedObject subclasses created in step 5.
    7. After that the project built again successfully and the problem disappeared.

    PS: If you choose to Manually create your NSManageObject subclasses rather then using Code Generation, then make sure you remove codeGenerationType="class" from your entities in the XML file in step 2.

    0 讨论(0)
  • 2021-02-04 05:49

    Problem 4 can be fixed (at least in the latest beta, v. 6) by selecting all the entities you want auto-generated, and then in the inspector clearing the Class -> Module field so that it defaults to "Global namespace".

    0 讨论(0)
  • 2021-02-04 05:49

    Regarding problem 2: Core Data's "optional" flag has nothing to do with the Swift concept of an optional. They are unrelated and do not mean the same thing. Marking a Core Data attribute as non-optional does not imply that it's non-optional as Swift defines the term. The difference is:

    • Swift non-optionals must have non-nil values at all times.
    • Core Data non-optionals must have non-nil values when you save changes, but Core Data neither knows nor cares whether they're nil at other times.

    Problem #4 sounds like Xcode getting its state confused, and may be fixed by voodoo like manually clearing the derived data folder (again, I know). I can't currently reproduce it, but that doesn't mean it's not a bug in the current beta.

    0 讨论(0)
  • 2021-02-04 05:51

    It's actually pretty easy to fix it.

    Go to configuration:

    Then delete the points. These are only there in older projects. The import statements are fixed after this and you are good to go.

    You also should put your models to the Global namespace. Mine were in a extra namespace, but i have no idea why.

    Apples approch is that you can use this in a custom framework.

    This feature is pretty nice, if you get it working ;)

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