Question

I referred this link as I am trying to sort the data from web service in table View. My first question is can we sort data in a mutable array? I get this compiler error in the following code.

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
NSArray *sortArrayWithObject = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [tempArray sortUsingDescriptors:sortArrayWithObject]; // error

tempArray is NSMutableArray and not NSArray.

Was it helpful?

Solution

Issue was because of a silly mistake. I modified API call as below and it now works.

NSArray *sortedArray = [tempArray sortedArrayUsingDescriptors:sortArrayWithObject];

OTHER TIPS

The method sortUsingDescriptors: returns void, that is why you get this message. The NSMutableArray to which the message is sent (in your case, tempArray) is directly modified by the sortUsingDescriptors: method.

It returns void, you are expecting NSArray from it.

- (void)sortUsingDescriptors:(NSArray *)sortDescriptors

Your code should be :

[tempArray sortUsingDescriptors:sortArrayWithObject]; 

Above will sort the receiving array (tempArray) using a given array of sort descriptors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top