1

I'm trying to sort an array, from a previous post I was pointed to an answer in this thread, Sorting an array of doubles or CLLocationDistance values on the iPhone

Based of this my code is as follows:

NSArray *sortedArray = [listArray sortedArrayUsingFunction:intSort context:nil];


NSInteger intSort(id num1, id num2, void *dummy)
{
    int v1 = [[num1 objectForKey:@"waypoint_order"] intValue];
    int v2 = [[num2 objectForKey:@"waypoint_order"] intValue];

    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

But its crashing on line int v1 = [[num1 objectForKey:@"waypoint_order"] intValue]; with 'objc_exception_throw'.

What am I doing wrong, I must be leaving out some functionality.

Regards, Stephen

4
  • What exception does it throw? Commented Sep 17, 2010 at 9:40
  • array out of bounds exception? Commented Sep 17, 2010 at 9:43
  • -[NSManagedObject objectForKey:]: unrecognized selector sent to instance 0x7a601f0. Commented Sep 17, 2010 at 9:43
  • Okay, I've just run a quick test and looped through my array output the contents of waypoint_order to the console. Details are displayed so not sure how its getting out of bounds. Commented Sep 17, 2010 at 9:46

2 Answers 2

1
+50

If these are NSManagedObjects, you want valueForKey:, not objectForKey:

Like this...

int v1 = [[num1 valueForKey:@"waypoint_order"] intValue];

Hope that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Firoze, thanks very much for this, I can't believe I missed it. I should have known that. I've been working a lot with NSManagedObjects lately.
1
  1. check num1 dictionary. is it autorelease or u release it somewhere in program.

  2. or check value return by Dictionary may be it returning nil;

  3. see linkArray object .at the time of creating u assigned last Value to nil or not . eg:[[NSArray alloc]initWithObject:object1,object2,object3,nil];

2 Comments

(1) Definitely not auto released and I'm not releasing it anywhere. (2) How do I do this ? (3) listArray is created from a NSFetchRequest and definitely no 'nil' value at the end.
Anybody any thoughts on this, I'm still having trouble.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.