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

I watched the tutorial ( https://www.youtube.com/watch?v=qt8BNhpEAok ) to use the basic data in Xcode 8, and the video used Swift. He went into an object called "Task" in the main data model, and then into the code that he was able to call "Task", for example:

let task = Task(context: context)

I have an object called "Tag". How can I access a tag in code using objective-c? CodeGen is installed in the underlying data model, but I do not see any additional files in my project. If i try

Tag test = [[Tag alloc] init];

I get a message that the tag does not exist.

+4
source share
2 answers
  • Select the tag tag in the model editor.

  • Create the source code for the task by selecting the menu tree "Editor" → "Subclass NSManagedObject ..." then follow the instructions.

'Tag + CoreDataClass.h'

'Tag + CoreDataClass.m'

'Tag + CoreDataProperties.h'

'Tag + CoreDataProperties.m'

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

  1. Import the header file.

#import "Tag+CoreDataProperties.h"

  1. Then create the "Tag" class.

NSManagedObjectContext* wContext = ((AppDelegate*)UIApplication.sharedApplication.delegate).persistentContainer.viewContext; Tag* w Tag = [ [ Tag alloc ] initWithContext: wContext ]; wTag.name = @"TEST";

+2
source

If Codegen is set to Class Definition, you can simply import the subclass header file NSManagedObject.

Import

#import "Tag+CoreDataClass.h"

Then the creation of your object will be recognized Tag.

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

.. , , DerivedData . .

- :

/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}/

Codegen, :

  • /: NSManagedObject . , .
  • /. (), .

Codegen: fooobar.com/questions/106979/...

+4

Source: https://habr.com/ru/post/1663692/


All Articles