Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 8 How to use Core Data Code Gen Classes in Objective-C?

I was watching this tutorial to use Core Data in Xcode 8 and the video was using Swift. In the video, He entered an entity named Task in the Core Data model and then in the code he was able to call Task like:

let task = Task(context: context)

I have an entity called Tag. How can I access Tag in code using Objective-C? Codegen is set in the Core Data model but I don't see any additional files in my project. If I try:

Tag test = [[Tag alloc] init];

I get an error that Tag does not exist.

like image 202
SolidSnake4444 Avatar asked Dec 14 '16 00:12

SolidSnake4444


People also ask

How do I use Core Data in Xcode?

Open Xcode and create a new iOS project based on the Single View App template. Name the app HitList and make sure Use Core Data is checked. Checking the Use Core Data box will cause Xcode to generate boilerplate code for what's known as an NSPersistentContainer in AppDelegate.

How do I use Core Data in an existing project?

Add a Core Data Model to an Existing ProjectChoose File > New > File and select the iOS platform tab. Scroll down to the Core Data section, select Data Model, and click Next. Name your model file, select its group and targets, and click Create.

What is Core Data in Objective C?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.


2 Answers

If Codegen is set to "Class Definition" then you can just import your entity's NSManagedObject subclass header file.

Import:

#import "Tag+CoreDataClass.h"

Then the creation of your Tag object will be recognized.

Tag *tag = [NSEntityDescription insertNewObjectForEntityForName:@"Tag" inManagedObjectContext:[self managedObjectContext];
tag.name = @"Tag Name";

Note: If you want to see the files that were generated on your behalf, you can check the DerivedData folder for your project. You should not edit these files or import them into your project.

Something like:

/Users/{Username}/Library/Developer/Xcode/DerivedData/{Your Project Name}/Build/Intermediates/{Your Project Name}.build/Debug-iphonesimulator/{Your Project Name}.build/DerivedSources/CoreDataGenerated/{Your Project Name}/

There are other Codegen options that offer different options depending on your use case:

  • None/Manual: Allows you to manage the NSManagedObject subclasses yourself. With this option, you will see the files in your project and you can modify them.
  • Category/Extension: Allows you to have custom properties (attributes) that you do not want Core Data to manage.

I posted a more detailed answer regarding Codegen options here: https://stackoverflow.com/a/40647786/4748172

like image 176
Ryan H. Avatar answered Sep 20 '22 12:09

Ryan H.


  1. Select the entity 'Tag' in model editor.

  2. Generate source code for Task by selecting menu tree 'Editor' -> 'Create NSManagedObject Subclass...' then follow the instruction.

'Tag+CoreDataClass.h'

'Tag+CoreDataClass.m'

'Tag+CoreDataProperties.h'

'Tag+CoreDataProperties.m'

files will be created and automatically will be attached to your project.

  1. Import header file.

    #import "Tag+CoreDataProperties.h"
    
  2. Then create 'Tag' class.

    NSManagedObjectContext *wContext = ((AppDelegate *)UIApplication.sharedApplication.delegate).persistentContainer.viewContext;
    Tag *wTag = [[Tag alloc] initWithContext:wContext];
    wTag.name = @"TEST";
    
like image 21
Satachito Avatar answered Sep 19 '22 12:09

Satachito