Duplicate Symbol Error in NSManagedObject Subclass - ios

I just simply created a demo project with Core Data.
I created an entity Userinfo in my data model. Now I created a NSManagedObject subclass of this entity.
Xcode autogenerated these 4 classes.
Now when I build the project it throws this error:
I have done everything I know to remove the error of duplicacy but nothing helped.
I think its a Xcode bug. Please help.

You are generating files which have already been generated for you by Xcode and thus get duplicate declarations. Details about this feature (new in Xcode 8) can be found in this WWDC video.
Two possible fixes:
1) Use the Xcode generated ManagedObject subclasses (the recommended, modern approach)
Delete all generated NSManagedObject subclasses from your project, if exists.
Set Codegento Class Definition in your .xcdatamodel for all entities
Make sure Module is empty ("Global Namespace" in light gray) (workaround an Apple bug, see this answer)
Clean project
Clean DerivedData folder (Optional. To be on the save side)
build
Note:
Never add the automatically generated files to your project. Even you do not see the generated files in your project, Xcode has a reference to it, so you are able to write extensions and such. For instance:
extension MyEntity {
func doSomething() {
//
}
}
Also, you can command+click to the generated file within Xcode.
2) Trigger subclass generation manually (a rather paranoid but bullet-prove approach, ignoring the new Xcode features)
Delete all generated NSManagedObject subclasses from your project, if exists.
Set Codegento Manual/None in your .xcdatamodel for all entities
Clean project
Clean DerivedData folder
Restart Xcode
Manually generate NSManagedObject subclasses (in "Editor" menu)
Make sure those files are added to your project
build

Related

core data invalid redeclaration of class

So, I have a got a problem…
when I intend to create a NSManagedObject Subclass.
As you can see this problem is really spread widely. Many people propose a solution by changing codegen block into Manual/None. But this did not help me, furthermore Class Definition and Category/Extension did not resolve too.
When I delete two files which were created by tapping in Product/Create NSManagedObject, the code is really works. I didn't why but I could use a NSManObject classes like they are lying somewhere I found a path where they are existing.
I thought If I delete them then I can recreate subclass again and use it successfully. But it is not. When I create again a subclass files these two files that were lying in unknown directory were recreated again! I ask the people who encountered to this problem and I need their help or solution
There's an annoying bug in the core data codegen settings, which means that updated settings aren't stored correctly.
If you change the codegen settings and then Build or Run, your changes will not be saved. For example, if you changed from Category/Extension to None and deleted the generated file, it will be recreated.
To solve this problem…
Change your codegen settings
Save the .xcdatamodel file.
Close all project windows.
Re-open the project.
I tested for different Core Data data model class codegen settings. When it's set to Class Definition or Category/Extension. The builder will generate subclass files automatically. So we don't need to add these files by ourselves. The following is files generated by builder.

can not create subclasses of managed objects

from documentation (What's New In Core Data)
Xcode automatic subclass generation
Xcode now supports automatic generation of NSManagedObject subclasses in the modeling tool. In the entity inspector:
Manual/None is the default, and previous behavior; in this case you should implement your own subclass or use NSManagedObject.
Category/Extension generates a class extension in a file named like ClassName+CoreDataGeneratedProperties. You need to declare/implement the main class (if in Obj-C, via a header the extension can import named ClassName.h). -
Class Definition generates subclass files named like ClassName+CoreDataClass as well as the files generated for Category/Extension.
The generated files are placed in DerivedData and rebuilt on the first build after the model is saved. They are also indexed by Xcode, so command-clicking on references and fast-opening by filename works.
and no matter what i chose it doesn't get generated.
i'm probably doing something wrong or incomplete, aren't i?
If you set "Codegen" to "Class Definition", like in your example, Xcode does generate the NSManagedObject subclasses, but you do not actually see the in your project. Like stated in your posted quote, the files are generated in
~/Library/Developer/Xcode/DerivedData/...
You do not see them in your project, only the files in Derived Data, which you should not have to care about. Xcode does hold a reference though, so command+clicking in code jumps to the implementation as expected plus you are able to write extensions and such.
Find details in this answer.

Error when creating NSManaged Subclass in Xcode 8.2 beta

I am getting this error that seems to warn me of duplicate files. Am I missing something or Xcode now doesn't require creating nsmanaged subclass to operate on core data. I tried creating a blank project and the same thing happens. Here is the error data
<unknown>:0: error: filename "Card+CoreDataClass.swift" used twice: '/Users/user/Documents/Developer/coredata/Card+CoreDataClass.swift' and '/Users/user/Library/Developer/Xcode/DerivedData/coredata-gicbdltjgiidbcfxaqqaxvkqwnnk/Build/Intermediates/coredata.build/Debug-iphonesimulator/coredata.build/DerivedSources/CoreDataGenerated/coredata/Card+CoreDataClass.swift'
<unknown>:0: note: filenames are used to distinguish private declarations with the same name
<unknown>:0: error: filename "Card+CoreDataProperties.swift" used twice: '/Users/user/Documents/Developer/coredata/Card+CoreDataProperties.swift' and '/Users/user/Library/Developer/Xcode/DerivedData/coredata-gicbdltjgiidbcfxaqqaxvkqwnnk/Build/Intermediates/coredata.build/Debug-iphonesimulator/coredata.build/DerivedSources/CoreDataGenerated/coredata/Card+CoreDataProperties.swift'
<unknown>:0: note: filenames are used to distinguish private declarations with the same name
Command /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
This implies to me that the files have already been automatically created. I can even make a Card object without manually creating the subclasses. Thanks.
The issue here is that as of Xcode 8, new projects using Core Data and new Core Data data models are configured to use automatic code generation by default. That means you don’t have to explicitly generate code for your Core Data entities any more, Xcode will take care of that for you. If you leave automatic code generation on and also manaully generate Swift classes for your entities, you’ll see an error like this.
You can disable automatic code generation for an entity in the entity inspector, or you can remove the manually-generated code from your project. Either of the two should fix this.
Try clearing your derived data via Xcode > Preferences > Locations > little grey arrow > move contents to the trash. Then clean the project and rebuild.
First check in you project that you have not imported this file Card+CoreDataProperties twice, also check that you have not created this class Card+CoreDataProperties twice throughout your project, also delete derived data and clean your project.

Xcode is looking for core data entity names with dot; not compiling

I have been working on a project for a while, and recently upgraded to Xcode 8 and Swift 3.0 and iOS 10. But since I did that I have not been able to compile.
I am getting an error for each of my entities:
:0: error: no such file or directory: ''/Users/mark/Library/Developer/Xcode/DerivedData/.../.Account+CoreDataProperties.swift'
Each case has a . (dot) prefix before the entity name: .Account+CoreDataProperties.swift.
I changed the Code Gen from "Category / Extension" to Manual / None, I do a clean and clean directory, an delete the DerivedData directory. Interestingly, when I look in the appropriate directory there is an actual file there, just without the dot prefix.
This is very confusing. Can anyone explain it? I need to solve this to be able to continue with core data.
TIA
Mark
The dot files are generated by Xcode8. See WWDC2016. I ran into the same issue after having to delete derived data due to another issue.
Two possible fixes:
1) The recommended, modern approach
Delete all generated NSManagedObject subclasses from your project, if exists.
Set Codegento Class Definition in your .xcdatamodel for all entities
Make sure Module is empty ("Global Namespace" in light gray) (workaround an Apple bug, see #Chris Hansons answer)
Clean project
Clean DerivedData folder
Note: Even you do not see the generated files in your project, Xcode has a reference to it, so you are able to write extensions and such. For instance:
extension MyEntity {
func doSomething() {
//
}
}
Also, you can command+click to the generated file within Xcode.
2) A rather paranoid but bullet-prove approach, ignoring the new Xcode features
Delete all generated NSManagedObject subclasses from your project, if exists.
Set Codegento Manual/None in your .xcdatamodel for all entities
Clean project
Clean DerivedData folder
Restart Xcode
Manually generate NSManagedObject subclasses (in "Editor" menu)
Make sure those files are added to your project
build
If your problem persists, repeat:
Clean project
Clean DerivedData folder
Restart Xcode
This occurs when the module of an entity is set to "Current Product Module" (e.g. to be within the Swift namespace, rather than the global Objective-C namespace).
The workaround for this is to remove the customization of the "Module" field of the entity, so it has the default value of "Global namespace" (in light gray text).
I changed the Code Gen from "Category / Extension"
Change Codegen to Class Definition.
Now get rid of whatever you were doing in code to turn your entities into pseudo-classes. Your entities are now real classes!
You will now be able to pare your code down considerably. You no longer have to cast down to specify an entity type as a class. When you fetch Person objects, your fetch results is a generic parameterized on Person, and so each fetched object is a Person. Similarly, to make a new Person, just call Person(context:) and configure, and save the store. The word "entity" will probably cease to exist anywhere in your code.
I have solved it. I was about to recontruct the whole app from scratch to avoid whatever the issue was, and I noticed that the entity class files were in the directory, even though they weren't visible in Xcode. So I deleted those files and that cleared that hurdle.
I'm very happy now.

Core data: Unable to load class named "CDAccount"

I've recently downloaded iOS 8 to make sure that my existing app works properly with it, but I'm getting a warning in the debugger on launch:
"CoreData: warning: Unable to load class named 'CDAccount' for entity 'CDAccount'.
Class not found, using default NSManagedObject instead."
After some research, it seems like people have had similar issues when using Core Data in Swift, but all my code is in Objective-C (and the data model hasn't changed for at least 6 months).
Any advice is much appreciated, I'm really nervous that my app isn't going to function properly once people start upgrading to iOS 8!
I had the exact same problem in Objective-C and Xcode 6. For some reason, Xcode had removed my 'CustomNSManagedObject'.m classes from my Compile Sources.
Go to your project target -> Build Phases -> Compile Sources and use the + button to add CDAccount.m
If you are using Mogenerator to create a _CDAccount.m file, add that to your Compile Sources as well.
Remove the "dot" in Configurations "Default" on .xcdatamodeld:
to:
Rebuild the application.
My issue was also similar, but the reason was that module of entity was not set.
It seems to be an Xcode bug/issue. I'm using Xcode 6.3.1
click on your core data entity (xxx.xcdatamodeld), then select the entity, and in the attributes inspector you'll see that the entity's class name is set to:
PRODUCT_MODULE_NAME.xxx, where 'xxx' is your entity's name.
solution : You have to replace PRODUCT_MODULE_NAME with your app's name.
If in doubt about your app's name, check under 'build settings', filter on 'product_module_name', and you'll find your app name under 'packaging'
in some cases (my case) the data model file (swift or objective-c) is also removed by Xcode from the project - it's still on in disk though, but you need to add it again. So in this case take the 'CDAccount.m' file & drop it in your project again (or use another of the several ways to do this)
My issue was similar, but the listed solutions didn't work because my CD Classes were stored in a framework. The solution was to change the Class Name Representation to "frameworkName.className". However, the xcdatamodeld didn't allow me to change it to that, so I had to "Show Contents" on the file, open the "contents" file in XCode, and make the changes myself.

Resources