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.
The problem is that you are trying to instantiate a
NSManagedObjectusing a conventional alloc/init method ofNSObject.If you want to use CoreData you have to use the NSManagedObject designated initialiser, as described on the class reference documentation for
NSManagedObjectSource: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html