Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData and NSInvalidArgumentException unrecognized selector sent to instance

I keep running into this exception when trying to set my property "phoneNumber":

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [SearchResult setPhoneNumber:]: unrecognized selector sent to instance 0x256b40'

Here's the class with the phoneNumber property:

@interface SearchResult : NSManagedObject
@property (nonatomic, retain) NSString * phoneNumber;
@end

@implementation SearchResult
@dynamic phoneNumber;
@end

Issue is that when I do this:

SearchResult *managedObject = [self findExistingSearchResultById:restaurantId];

if(managedObject == nil)
{ 
    managedObject = [NSEntityDescription insertNewObjectForEntityForName:@"SearchResult" inManagedObjectContext:managedObjectContext];
}

// Exception throws here.
managedObject.phoneNumber = @"1234567890";

Here's the findExistingSearchResult method:

+ (SearchResult *)findExistingSearchResultById:(NSString *)restaurantId
{
    NSManagedObjectContext *managedObjectContext = serviceContext;
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SearchResult" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"restaurantId = %@", restaurantId];
    [request setPredicate:predicate];

    NSError *error = nil;
    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:request error:&error];
    if(fetchedObjects != nil)
    {
        return [fetchedObjects lastObject];
    }
    else
    {
        return nil;
    }
}

I'm using very similar class implementations across my app, and not running into any issues like this. I haven't figured out why setting phoneNumber property here throws this exception.

like image 334
mservidio Avatar asked Jan 17 '23 15:01

mservidio


1 Answers

The problem is that you are trying to instantiate a NSManagedObject using a conventional alloc/init method of NSObject.

If you want to use CoreData you have to use the NSManagedObject designated initialiser, as described on the class reference documentation for NSManagedObject

NSManagedObject is a generic class that implements all the basic behavior required of a Core Data model object. It is not possible to use instances of direct subclasses of NSObject (or any other class not inheriting from NSManagedObject) with a managed object context. You may create custom subclasses of NSManagedObject, although this is not always required. If no custom logic is needed, a complete object graph can be formed with NSManagedObject instances.

A managed object is associated with an entity description (an instance of NSEntityDescription) that provides metadata about the object (including the name of the entity that the object represents and the names of its attributes and relationships) and with a managed object context that tracks changes to the object graph. It is important that a managed object is properly configured for use with Core Data. If you instantiate a managed object directly, you must call the designated initializer (initWithEntity:insertIntoManagedObjectContext:).

Source: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html

like image 199
Rog Avatar answered Feb 12 '23 02:02

Rog