Xcode 8 generates broken NSManagedObject subclasses for iOS 10

后端 未结 22 834
栀梦
栀梦 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:50

    What you need to do is set the CodeGen to Category/Extension before you execute the Create NSManagedObject subclasses.

    0 讨论(0)
  • 2020-11-27 11:52
    1. To solve this problem delete the derived data of the app.

    2. Then change the Module to Current Product Module and also make changes in Codegen to Manual/None.

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

    Xcode 8.1 seems to be generating Model Class internally. Just delete the 2 classes created and you will still be able to use the entities in your code.

    Here's the error message I was receiving

    <unknown>:0: error: filename "Recipe+CoreDataProperties.swift" used twice: '/Users/Rick/Desktop/Devslop/Rick Recipe/Recipe+CoreDataProperties.swift' and '/Users/Rick/Library/Developer/Xcode/DerivedData/Rick_Recipe-cctgjudvqobxlwetbcwmzrgxigwg/Build/Intermediates/Rick Recipe.build/Debug-iphonesimulator/Rick Recipe.build/DerivedSources/CoreDataGenerated/Rick_Recipe/Recipe+CoreDataProperties.swift'
    <unknown>:0: note: filenames are used to distinguish private declarations with the same name
    Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
    
    0 讨论(0)
  • 2020-11-27 11:53

    Also, if you add NSManagedObject subclass yourself, don't forget to follow the following step: Go to '.xcdatamodeld' -> Choose the entity -> Show data model inspector -> Codegen -> Choose 'Manual/None'

    This worked for me.

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

    I'm putting my grain of salt given that I can see a lot of answers that solve the problem but are lacking the real reason behind what's really happening here.

    I stumbled upon this very same issue and by doing a quick research I was able to find that:

    • Until Xcode 7, the default behavior for a Core Data entity was that the developer had to manually create and maintain the changes of the corresponding NSManagedObject subclasses. (This could, and still can be accomplished by making use of the editor menu selecting the "Create NSManagedObject subclass.." option).

    • Starting from Xcode 8, Apple has included a new setting right in the Data Model Inspector named Codegen to manage and maintain the NSManagedObject subclasses. The default value for this setting is "Class Definition" which tells Xcode to generate the NSManagedObject subclasses based on our defined entities in compile time and put them in the derived data folder.

    Having our NSManagedObject subclasses within the project + the ones Xcode automatically generates lead us to the real reason behind the "Multiple commands produce..." compiling time error as we now have duplicated managed objects models!!!

    The solution then is to use either one or another, not both! I personally prefer to have my managed object models within the project so I have changed this "Codegen" setting to "Manual/None". If you are not adding logic to the models I bet you could opt by removing your project's managed object models and let Xcode do its thing based on the "Class Definition" value for the mentioned setting. This is the right way to deal with this issue.

    You can find a great article explaining all of this in detail here:

    https://medium.com/@kahseng.lee123/core-data-codegen-explained-462c30341041

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

    For me, I created CocoaTouch classes by subclassing of NSManagedObject manually. I set the module as Current Product Module and Codegen as Category/Extension and it works well.

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