当我试图设置我的“phoneNumber”属性时,总是遇到这个异常:
* 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因是:“- [SearchResult setPhoneNumber:]:向实例0x256b40发送了无法识别的选择器”
这里是具有phoneNumber属性的类:
@interface SearchResult : NSManagedObject
@property (nonatomic, retain) NSString * phoneNumber;
@end
@implementation SearchResult
@dynamic phoneNumber;
@end
问题在于当我执行以下操作:
SearchResult *managedObject = [self findExistingSearchResultById:restaurantId];
if(managedObject == nil)
{
managedObject = [NSEntityDescription insertNewObjectForEntityForName:@"SearchResult" inManagedObjectContext:managedObjectContext];
}
// Exception throws here.
managedObject.phoneNumber = @"1234567890";
这是 findExistingSearchResult 方法:
+ (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;
}
}
我在我的应用程序中使用非常相似的类实现,并且没有遇到像这样的任何问题。我还没有弄清楚为什么在这里设置phoneNumber属性会抛出此异常。