0
votes

Below is my viewDidLoad method in a tableViewController. When viewDidLoad runs this error comes up

2014-03-03 12:44:54.904 SalesCRM2[30188:70b] -[_PFArray sortUsingDescriptors:]: unrecognized selector sent to instance 0x8c45710 2014-03-03 12:44:54.931 SalesCRM2[30188:70b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_PFArray sortUsingDescriptors:]: unrecognized selector sent to instance 0x8c45710'

on this line of code

[array sortUsingDescriptors:[NSArray arrayWithObject:sort]];

Here is the whole method

 - (void)viewDidLoad
    {
    [super viewDidLoad];

    JCAppDelegate *appDelegate =
    [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context =
    [appDelegate managedObjectContext];

    NSEntityDescription *entityDesc =
    [NSEntityDescription entityForName:@"Customers"
                inManagedObjectContext:context];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    NSError *error;
    NSArray *objects = [context executeFetchRequest:request
                                              error:&error];

    NSMutableArray *array = (NSMutableArray *)objects;
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
    [array sortUsingDescriptors:[NSArray arrayWithObject:sort]];

    if ([objects count] == 0)
    {
        //_isEmpty = YES;
    }
    else
    {
        //_isEmpty = NO;
        _resultsArray = (NSMutableArray *)objects;



        NSLog(@"resultsArray: %i",[_resultsArray count]);
        //        matches = objects[0];
        //        _address.text = [matches valueForKey:@"address"];
        //        _phone.text = [matches valueForKey:@"phone"];
        //        _status.text = [NSString stringWithFormat:
        //                        @"%lu matches found", (unsigned long)[objects count]];
    }



}
1

1 Answers

0
votes

Read the error message. It's telling you the problem. You can say sortUsingDescriptors: to an immutable array. It is immutable.

Now, as for what you are doing wrong, it is much more interesting! You are saying:

NSMutableArray *array = (NSMutableArray *)objects;

Perhaps you believe that this turns an immutable array into a mutable array. It doesn't. You can't turn a silk purse into a sow's ear by typecasting. You may lie to the compiler (and you did, by typecasting to a false class), but you can't lie to the runtime. What an object is, that's what it is, no matter what you call it.

If you want a mutable array, you must make a mutable array (e.g. by calling mutableCopy) - it isn't enough to say a thing is a mutable array when in fact it isn't.