How to add core data classes to a framework in iOS - iphone

I am trying to create a framework out of my existing codebase for the purpose of reuse. I have a exmaple.xcdatamodeld class which in turn contains some core data classes. I have added this entire exmaple.xcdatamodeld class into my new framework. But i am getting an error which says that some of the classes in my exmaple.xcdatamodeld are missing. (Example:classname.h file not found). Am i doing something wrong while adding the entire core data class? Can anyone help me out with my problem...
I have taken this link as a reference while creating the framework
edit : I am not getting the option of adding all the core data classes individually. It seems i can add only the complete datamodel file. PLease refer the screenshot below.

exmaple.xcdatamodeld is your data model. If it refers to any classes that you've created (which will be subclasses of NSManagedObject), you'll need to include the source files (.h/.m files) for those classes in your framework.

The .xcdatamodeld file is just the model, a serialized version of the NSManagedObjectModel. It does not contain any classes.

Related

Codegen for CoreData entity not working in Xcode 11

So I am pretty new to CoreData in Xcode and I have been looking for tutorials to see how to get more familiar with it and start working with it. A tutorial I have been watching, mentions that we need to Generate code for an entity that I created. I select Editor -> Create NSManagedObject Subclass to generate the code for the entity. But the files generated come full of errors and warnings that I haven't caused. I will attach screenshots for some more details.
That's the entity that I am creating in CoreData
That's the first generated file for the entity
That's the second file generated for the entity
The below are the errors and warnings I am getting:
File 'Person+CoreDataClass.swift' is part of module 'CoreData'; ignoring import
File 'Person+CoreDataProperties.swift' is part of module 'CoreData'; ignoring import
Only classes that inherit from NSObject can be declared #objc
Cannot find type 'NSManagedObject' in scope
Cannot find type 'NSFetchRequest' in scope
Note that I have enabled CoreData when I first created the project and I haven't made any changes to any other files in the project.
I would appreciate any kind of help since I haven't got any experience yet with CoreData. Thank you!
Try creating a new project with a different name (not CoreData)

Xcode 11 doesn't recognize Core data Entity

I just declared an entity called "Users" array:
var UsersArray = [Users]()
I got this error:
use of unresolved identifiers "Users"
hint : I did import CoreData and created the entity
Please try this solutions !!
Solution 1 :-
Product->Build for testing solved this issue for me(it solves most unresolved identifier bugs for some strange reason)
Solution 2 :-
changing import Foundation to import UIKit.
It seems like UIApplication isn't included in Foundation framework.
Solution 3 :- Highlight the Data Model, go to Editor -> Create NSManagedObject Subclass...
Solution 4 :-
Create the NSManagedObject files and then delete them and everything is fixed.
Hope this helps thank you.
If your entity is actually created, just close and re-open XCode. This works for me everytime. The other solutions, such as clean build, build etc, didn't work for me.
Just ran into this issue after renaming an entity and figured I should post here how I resolved it.
Typically after making changes in the data model and getting compilation errors, I just do Product > Clean Build Folder, then Product > Build and that should take care of the errors.
However, the steps above didn't help this time. After scratching my head for while, I found the culprit with following steps:
In the Project navigator, select your .xcdatamodelId file
In the Configurations section, select the appropriate configuration
Look for the entity in question and make sure the class name is correct (in my case, the entity and the class were not the same)
Other possible solutions:
(A) Re-launch Xcode
This is the solution that worked for me. (See this answer)
(B) Check if you did not forget to finalize your entity description
Typically: forgot to assign a type to one of the attributes (still "undefined").
(C) Clear derived data
Reason: The generated core data managed objects subclasses's source code files are in the project’s derived data location.
Note: you might have to re-link your swift packages is using SPM.
I also had to deal with this issue even working with the latest Xcode12.3 on a SwiftUI project where I didn't checked the CoreData option at project creation.
After adding a class to instantiate the NSPersistentContainer, loading the stores, creating model.xcdatamodel file with an entity and a text field plus a button on a view, then it shows an error saying: "cannot find Item in scope".
It's a bit difficult to write code when you expect the entity to help out with autocomplete. First, I thought I forgot to initialize my class or missed a bracket, but it wasn't that either.
I tried cleaning up, rebuilding and running the project several times with build failed as result. I had to close this particular project then all errors got dismissed. No needed to restart Xcode.
I faced the same issue. Doesn't included Coredata on the beginning of the project. Later added necessary files and core data stack. But when trying to fetch the data from the Entity name and the Xcode throwing the same error.
Later, I closed the project and again opened on Xcode and it started to work.
I had this issue with an .xcdatamodel that I had created in another project and moved it into this one.
I solved it by selecting the .xcdatamodel file, turned of the "File Inspector" and then under "Target Membership", I made sure that all the targets were selected.
for me the entity name and class name was different, and I was refering to entity name for Data Type

TypeAlias a class contained in NSKeyedArchiver file

My app uses a file created with NSKeyArchiver, it has a reference to an object that is contained in an external Framework. The external Framework is no longer is available in the app Xcode project. So when I attempt to load the archive file, the app will crash when attempting to resolve the type of this object. i.e MyMobileKit_Token
I was wondering if it's possible to create a 'local' instance of the object type in the app, then using the typealias fool the unarchive operation to reference the local type i.e Token
Or would I have to create a new 'proxy' Framework project with this one class and add the proxy framework to my app project keeping the naming of the framework and class exactly the same.
Any ideas?
Thanks
Aha, I found the answer. I created a class called Token with the same properties in the framework. Then I added the following line before opening the archive file:
NSKeyedUnarchiver.setClass(Token.self, forClassName: "MyMobileKit.Token")
Seemed to work out.

add scaffolded item wizard cannot resolve data model class

everyone.
I'm trying to create new Scaffolded item for simple ASP.NET Wep API application which should be based on domain object class and DbContext derivative in separate assembly. The assembly is in the solution, target app has a reference to it (and manually created code which invokes the classes from my lib is build up without any errors) and, obviously, the classes I've mentioned have access modifier public.
The problem is wizard for creating new scaffolded item cannot see my model classes. (By the way, when the model classes were in another ASP.NET MVC5 app the wizard worked well.) I'm using Visual Studio 2013 Update5.
How to fix this? Any workaround would be helpful too!
Sorry for disturbing.
the origin of the problem seems to be that I've move my data model classes from ASP.NET MVC app into my class library in wrong way.
I've not just cut/paste them, but copy-paste, cut-paste (confirmed replacement).
this is the only thing which could cause the problem (although I still don't know the details).
Never the less I've started from the beginning again and cut/past the data model files initially - everything seems to work fine now.

Using Mogenerator

I've have been using catagories up until now but with the start of a new project I had a look around for new toys to play with and found mogenerator.
I've managed to generate the _entity and entity files, great. But now what?
Am I supposed to put all my code in entity? Do I have to manually add all the new properties now that I cant use the generate nsmo subclass? How do I use the additional methods in _entity, must I import the _entity or the entity files or both?
So for anyone encountering the same problem.
Things seemed to be going wrong because the Entity classes should inherit from the _Entity class. I didnt pick this up from any of the guides but it seems obvious now! Then the Entity class can be a blank implementation if you like.