Unrecognized selector sent to instance when calling NSManagedObject subclass self-made method

Asked 2 years ago, Updated 2 years ago, 123 views

Xcode Version 8.1(8B62) DeploymentTarget iOS 8.4
We are creating a process to add data to Core Data.
The Xcode feature automatically generated subclasses and added self-made methods.

Data Model Created with Xcode

//Media+CoreDataClass.h
@interfaceMedia:NSManagedObject
    -(void) import:(NSArray*) received;
@end

@property(nullable, nonatomic, copy) NSSstring*id;
@property(nonatomic)int16_tdiskNo;
@property(nullable, nonatomic, copy) NSSstring* title;

// Media+CoreDataClass.m
-(void) import:(NSArray*) received {

    self.id = [received valueForKey: @"id" ];
    self.diskNo = [(NSNumber*) [received valueForKey:@"diskNo"] intValue];
    self.title = [received valueForKey: @"title";
}

The following is a successful completion, but

appDelegate_=(AppDelegate*)[[UIApplication sharedApplication]delegate];
context_=appDelegate_.persistentContainer.viewContext;
Media*media=[NSEntityDescription insertNewObjectForEntityForName:@"Media"
                                             inManagedObjectContext:context_];
media.id=[json valueForKey:@"id"];
media.diskNo=[(NSNumber*)[json valueForKey:@"diskNo"]intValue];
media.title = [json valueForKey: @"title";
appDelegate_saveContext;

If you make a method call as follows, you will get an error.

appDelegate_=(AppDelegate*)[[UIApplication sharedApplication]delegate];
context_=appDelegate_.persistentContainer.viewContext;
Media*media=[NSEntityDescription insertNewObjectForEntityForName:@"Media"
                                             inManagedObjectContext:context_];
media import:json;
appDelegate_saveContext;

- Media import:—Unrecognized selector sent to instance 0x6000002c2370
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [Media import:]: unrecognized selector sent to instance 0x6000002c2370'

It was written on various sites that it is possible to add self-made methods to the subclass of NSManagedObject, but please let me know if there is anything wrong with the method.

xcode objective-c coredata

2022-09-30 21:26

1 Answers

If you want to generate a subclass of an NSManagedObject to add functionality
1. Create a data model
2. Make Class-Codegen Manual/None
Manual/None
3. Select Editor-Create NSManagedSubclass from the menu
4. Add functionality to the generated subclass
5. Compilation links

If CoreData is used without generating subclasses (without adding features)
1. Create a data model
2. Set Class-Codegen to Class Definition
Class Definition
3. Compilation link

If a subclass is generated in Create NSManagedSubclass from the menu, an error will occur at the time of link that a class of the same name already exists. (Of course, if you remove a set of subclasses from the compilation link, you will get an unrecognized selector to instance😅)


2022-09-30 21:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.