为什么我会收到NSInvalidArgumentException?

时间:2011-10-29 19:58:59

标签: objective-c

为什么我的代码下面会出现NSInvalidArgumentException?

  

由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [订购菜单]:无法识别的选择器发送到实例

(void)fetchDishRecords: (NSMutableArray*)array predicate: (NSString*)categoryname 
{
// Define our table/entity to use      
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Order" inManagedObjectContext:managedObjectContext]; 

// Setup the fetch request      
NSFetchRequest *request = [[NSFetchRequest alloc] init];      
[request setEntity:entity];

//set predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Category=%@", categoryname];    
[request setPredicate:predicate]; 

//set sorting
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Dish" ascending:YES];      
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];      
[request setSortDescriptors:sortDescriptors];      
[sortDescriptor release]; 

// Fetch the records and handle an error      
NSError *error;      
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 
if (!mutableFetchResults)     
{          
    // Handle the error.      
}     
else    
{
    for(int i=0;i<[mutableFetchResults count];i++)        
    {            
        Order *orderObj = [mutableFetchResults objectAtIndex:i]; 
        //NSLog(orderObj.dish);
         //NSString* tempStr = orderObj.dish;
        [array addObject:orderObj.dish];        
    }
}    
// Save our fetched data to an array      
[mutableFetchResults release];      
[request release];

1 个答案:

答案 0 :(得分:2)

[array addObject:orderObj.dish];        

似乎是罪魁祸首...... orderObj是Order类,错误说它不能执行选择器菜。

确保为Order类实施了方法-(void)dish