I'm trying to conform to the MVC paradigm in my iOS app. My model consists of entities stored in coreData which I have set up using the xcdatamodel file.
I want to add some custom methods to these entities in order to keep the Model part separate from the Controller.
In previous versions of Xcode the autogenerated managedObject classes were added automatically to my project and I could add custom methods to these classes. Now I no longer see these auto genterated classes.
I have selected 'Class Definition' for the codegen.
Do I need to create an additional class for each entity to enable custom methods on those entities? Or is there a better way to go about this.
This is a rather new Xcode feature. Xcode generates the files for you and holds a reference to the generated files. Even the files do not show up in the project, you still can write extensions for the generated classes.
You can find more detailed information in this answer.
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.
I am working on a project which contains multiple targets. This project is using CoreData but each target's needs are different enough to where I thought it would be useful to have a separate Core Data models (eg. TargetName.xcdatamodeld) for each target. And all models would share a common entity with the same class file. I would like to know if any other developers have used this pattern?
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 know previously you would have to "Editor" > "Create NSManagedObject Subclass" in order to reference your Core Data's entity names in your classes. However I'm given several errors after creating these automatic subclasses.
My project runs fine without these subclass files, so does Swift 3 not require subclassing to reference entities anymore?
In Swift 3 you don't need to manually create subclasses for NSManagedObjects.
By default they are created automatically. But if you want manually create files, set Codegen to Manual/None in Utilities area for your testCD.xcdatamodeld.
From Apple's 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.
Is anyone else having problems getting VS2013 to recognize Models from an external class library? I really don't want to have to manually create every view and every Action when adding new Controllers and Views. My ViewModels are also in an external .dll so the only option I really have is creating new ViewModels in current project and use some sort of AutoMapper to reduce grunt work. Thanks in advance.
Here is screen shots:
1. Add Controller:
2. Recognize Model and Context
Solution
Thanks to Sarbanjeet I thought I would share the steps taken. The projects must be referenced in order to perform the typical Add Views/Controllers with CRUD using EF in VS2013.
1. Add Existing Project
**2. Add Reference (csproj) not dll
Rebuild Main Project**
This makes sense since the class dll only contains the compiled classes while the project file contains data on the project itself.
Include external library dll in References folder using these steps.
Right click on References folder.
Click on Add Reference.
Then browse the project library and add it to references folder(Remember project reference not class reference).
Clean solution and rebuild it or unload project and reload it.
If no luck, try these methods Click here