6

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.

shim
  • 9,289
  • 12
  • 69
  • 108
SolidSnake4444
  • 3,372
  • 6
  • 40
  • 63
  • If you open your `xcdatamodeld` file in your project and select your "Tag" entity, what is the Codegen option that is selected? If it is set to "manual/none", then the `NSManagedObject`subclasses will not be generated when you build your project. – Ryan H. Dec 14 '16 at 02:47
  • If it is set to "Class Definition" then the `NSManagedObject` subclasses will be generated for you when you build the project, however they will not show up in your project. – Ryan H. Dec 14 '16 at 02:59
  • @RyanH. It is set to "Class Defintion". If it is in the project, how do I access them? I selected global namespace in the options so I thought I could just say Tag and have it show as a class. Do I need to import something in the header file? – SolidSnake4444 Dec 14 '16 at 14:26
  • That is correct, you have to import the header file for automatically generated `NSManagedObject` subclasses. I have posted an answer with more details. – Ryan H. Dec 14 '16 at 17:01

2 Answers2

5

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

Community
  • 1
  • 1
Ryan H.
  • 2,543
  • 1
  • 15
  • 24
3
  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";
    
shim
  • 9,289
  • 12
  • 69
  • 108
Satachito
  • 5,838
  • 36
  • 43
  • Does this insert the tag into the database immediantly? The initWithContext line. – SolidSnake4444 Dec 14 '16 at 21:26
  • I ended up not needing to do step 2. The magic I needed was step 3, I did not know that class was created. I'm guessing step 2 might make it show up in the project. Step 4 did insert the tag into the database so everything works the way I thought it would. Thank you. – SolidSnake4444 Dec 14 '16 at 23:10