invalid redeclaration in auto code generate NSMana

2019-01-17 05:15发布

Using Version 8.1 of Xcode.

Create an entity named "MapRegionObject" in .xcdatamodeld file.enter image description here

Using auto code generator, click Editor on the navigation bar -> create NSManagedOject Subclass...

Got two files : MapRegionObject+CoreDataClass.swift and MapRegionObject+CoreDataProperties

Errors in two files showing in the screenshot: MapRegionObject+CoreDataClass.swift enter image description here

MapRegionObject+CoreDataProperties enter image description here

Please help me fix this bugs, thank you so much!

9条回答
女痞
2楼-- · 2019-01-17 05:58

In Xcode 8.1, before using the auto code generator, you must select the entity in your data model:

Entity

Then go to the Data Model Inspector tab:

Data Model Inspector

Under "Codegen" select "Manual/Node"

After that you could create a NSManagedObject subclass without errors.


Alternatively, if you have already used 'Class Definition', you can go into your existing .xcdatamodeld file and set all current entities to 'Manual/None' under Codegen. Make sure to save your project (File -> Save), delete your existing Derived Data, clean the project, and then build. Resolved it for me without having to re-make my whole model.

查看更多
做个烂人
3楼-- · 2019-01-17 05:58

This isn't an answer. It's just explanation of what's going on with the selections

Make sure you see this moment for the Core Data Stanford course

Below is a transcript I wrote myself (It's not 100% accurate):

The default is class definition, if you choose this one. It will generate that subclass and it will just work. You'll be able to access your tweets as a class called Tweet. This sounds good. Btw if you do this it will NOT show up in your file navigator.

The one we choose the most often is the category/extension what this will do it will only generate an extension of the Tweet class. You have to actually write the tweet class itself. The extension will take care of making all the vars. Even when I switch to category/extension again I don't get that extension showing up in the navigator. It's kind of hidden from you.

And why do we like this one? Because a lot of times we want to add our own code. Like in a Tweet, imagine you want to add a static method that took data from Twitter and turned it into a tweet in the database. Where would we put code? Well a great place to put that code would be in the Tweet class...if there was such a thing...and the extension is going to handle all the var business for you.

If you did choose manual/none for codegen. meaning don't do any codegen, then you would be doing value/setValue(forKey:)...good luck with that You're code is going to be a mess. [ie there is no .propertyName = value ...you'd had to do setValue(value, forKey: propertyName)].


Long story short, I'm not sure why but for some reason if you don't select the create NSManagedObject subclass then it seems to still work, but without showing what's happening under the hood. Very counter intuitive!


Then also watch this live demo of the Core Data Stanford course:

Now we know we want to access all this stuff not using value/set(value:forKey:)...we want to have to subclasses of Users/Tweets. And we want to have vars [ dot notation] for all these relationships so we need that code to be generate. The way we do that we just select the entity... and we go down here to CodeGen. This says by default class definition. That means it's done it. It has generate a class called Tweet. and It's going to work with var and all relationships. That's not actually what we want. We want to select this one [Category/Extension]. Where only create an extension to Tweet and add the var stuff. That's because we want to write the class Tweet and put our own code in there. It's very common to write our own class. But you still want the var magic.

查看更多
叛逆
4楼-- · 2019-01-17 06:01

In Xcode 8.2.1 , Menu-Product-Clean,And every is ok, it's so verid.

查看更多
贪生不怕死
5楼-- · 2019-01-17 06:04

The problem is that you don't need to generate NSManagedObjectModel subclasses manually anymore. ref: https://forums.developer.apple.com/thread/48988

Xcode automatically generates classes or class extensions for the entities and properties in a Core Data data model. Automatic code generation is enabled and disabled on an entity by entity basis, and is enabled for all entities in new models using the Xcode 8 file format. This feature is available for any data model that has been upgraded to the Xcode 8 format. You specify whether Xcode generates Swift or Objective-C code for a data model using the data model’s file inspector. When automatic code generation is enabled for an entity, Xcode creates either a class or class extension for the entity as specified in the entity's inspector: the specified class name is used and the sources are placed in the project’s Derived Data. For both Swift and Objective-C, these classes are directly usable from the project’s code. For Objective-C, an additional header file is created for all generated entities in your model: The file name conforms to the naming convention 'DataModelName+CoreDataModel.h'.

查看更多
甜甜的少女心
6楼-- · 2019-01-17 06:06

Close the project and follow the following instructions:

  1. Reveal in finder your database .xcdatamodeld file.
  2. on .xcdatamodeld file right click -> Show Package Contents, if(.xcdatamodel) is find again in the package again right click and 'Show Package Contents'. you should get 'contents' file.
  3. Open 'contents' in text edit.
  4. Command-F (codeGenerationType="class") and replace all matchings string with blank string.

Save and open Xcode project again.Everything should work well.

查看更多
beautiful°
7楼-- · 2019-01-17 06:10

1) clean the project (cmd + shift + K)

2) In the "data model inspector" for every created Entity set attributes for Class just as in the screenshot below

screenshot

3) Generate code again (Editor -> create NSManagedObject Subclasses)

After that everything should work fine.

查看更多
登录 后发表回答