When i create Core Data NSManagedObjectSubclass somehow Core Data created files, that i actually cant find. You see, in my project i have
#import "Menu+CoreDataClass.h"
#import "Menu+CoreDataProperties.h"
And if i use cmd+click on them, i actually opening this files. However, there is no such files in my app, i cant event find them with spotlight on mac.
Problem is, when i try to create actual files, compiler through me an error - duplicate symbols for architecure, etc..
So, my question is quite simple, how to find and get rid of this files?
Xcode will generate these files for you and store them in the derived data directory of your project. It automatically (most of the time) regenerates them if you change the model.
To change this behaviour, in the model editor change the Codegen option in the data model inspector to Manual / None, and generate the files yourself manually. You may need to clear derived data to prevent the duplicate symbol error.
These files are created automatically by Xcode.
Xcode:Entity codegen property
You need to select Manual/None codegen property for your entity. After this make product clean.
Related
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.
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.
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.
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.
I'm toying a lot with core data lately, and what bugs me is that you create a .xcdatamodeld to create your base structure, but then xcode convert it in a .momd file
My question is this : is it possible to do the conversion programmatically, instead of having it done by xcode?
Let's say for instance we wan't to be able to download a xcdatamodeld file from internet in an iOS app, and have it interpret it and create the underlying base.
You can create a momd file starting from an xcdatamodeld source using the momc script from Apple.
An xcdatamodeld “source” directory is compiled into a momd deployment directory, and an xcdatamodel “source” file is compiled into a mom deployment file.
(As explained here https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coredata/articles/cdUsingMOM.html)
How to use the script:
Make a copy of the xcdatamodeld file you want to transform. It will be replaced.
Open the terminal and go to the Developer bin folder
cd /Applications/Xcode.app/Contents/Developer/usr/bin/
Run the momc script on your xcdatamodeld file
Usage: momc source destination
Example (it's important to put extensions):
./momc input.xcdatamodeld result.momd
If you download the entire xcdatamodeld bundle (it's a directory) and unpack it, you can use code from my momcom project to compile it into a .momd. You could then load that like any other data model. This project is still somewhat experimental, but it's performed well in testing so far. [Update: the project won't work on iOS as it is, because it uses NSXMLDocument. If you wanted to try this, you could use TouchXML, which is designed to be a drop-in replacement for NSXMLDocument. I don't know of any other options for compiling a Core Data model in your own app.]
The usual rules about having a data model and a persistent store match up apply. You couldn't just take the newly compiled model and use it with an existing store unless you handled model migration. Also, I have no idea what Apple's attitude would be regarding an app that downloaded new data model files-- it might be fine, or it might get you into some trouble with them.
No you can't download a xcdatamodeld file from internet and put it working. If you do so your programme will crash because of wrong data model. your data model needs to be in the main bundle and you have to select current data model. Core data maintain model version and some information about your data model. Even if you change your model and do not migrate the model your programme will crash and you have to delete the application from your device/simulator.
However, you can download the .sqlite database but data model should be the same.