简体   繁体   中英

Why do I get a NSInvalidArgumentException?

Why do I get a NSInvalidArgumentException with my code below?

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Order dish]: unrecognized selector sent to instance

(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];
[array addObject:orderObj.dish];        

Seems to be the culprit... orderObj is of class Order and the error says it cannot perform selector dish.

Make sure you have a method -(void)dish implemented for your Order class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM