0

I am trying to sort an NSMutableArray for that I am using comparer selector;

- (NSComparisonResult)compare:(Product *)someObject {
    return [self.Price compare:someObject.Price];
}

HomeProducts = [HomeProducts sortedArrayUsingSelector:@selector(compare:)];

I want to sort the same array and donot want to assign to other array.

request for member 'Price' in something not a structure or union .... 

its not recognizing self.Price.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Azhar
  • 20,500
  • 38
  • 146
  • 211
  • What object you store in NSMutableArray ? Can you plz provide code for the same ? – Janak Nirmal Aug 30 '11 at 11:46
  • http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it try this one... – shivangi Aug 30 '11 at 12:12
  • http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it see this link may be it helps you.. – shivangi Aug 30 '11 at 12:13
  • 1
    use NSSortDescriptor http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it – shivangi Aug 30 '11 at 12:15

2 Answers2

1

Assuming that compare: is in the Product class then you need to use the console to investigate further.

When the exception occurs, what are the values of self and someObject - I bet that one of them is either not a Product or something that's been released (I think the latter!)

When the crash occurs, you can check the values of self and someObject in the console like this :

po self
po someObject

(or use the gui to the left of the console).


EDIT Answer to comment :

sortedArrayUsingSelector: returns an NSArray, HomeProducts is an NSMutableArray. Try this instead :

HomeProducts = [[HomeProducts sortedArrayUsingSelector:@selector(compare:)] mutableCopy];
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • I put compare: in Product class and that error removed but now its another warning which cause exception : warning: incompatible Objective-C types assigning 'struct NSArray *', expected 'struct NSMutableArray *' – Azhar Aug 30 '11 at 12:00
1

How you use it, compare: should be a method of Product. Try to use a block instead:

[homeProducts sortUsingComparator: ^NSComparisonResult (id obj1, id obj2)
{
    return [[obj1 Price] compare: [obj2 Price]];
} 

or, if Price is a simple type, use:

[homeProducts sortUsingComparator: ^NSComparisonResult (id obj1, id obj2)
{
    return ([obj1 Price] < [obj2 Price]) ? NSOrderedAscending :
           ([obj1 Price] > [obj2 Price]) ? NSOrderedDescending :
                                           NSOrderedSame;
} 

FWIW, it is customary to give instance variables, variables, properties, etc. a name starting with a lower case character, and only types start with an uppercase letter.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94