0

I need to iterate through an NSArrayController in a coredata model by a specific order.

Lets say that my model has these columns:

animal (string)
name (string)
age (int)

and I would like to iterate trough the controller like this:

for (animal *a in myNsArrayController.arrangedObjects)
{
  // Rest of code
}

but I would need to do it ordered by animal age... how can i do that?

A search did not bring me to any usable results..

Firo
  • 15,448
  • 3
  • 54
  • 74
sharkyenergy
  • 3,842
  • 10
  • 46
  • 97

2 Answers2

2

Try this, it builds off of AKV's answer but uses your code and contains the for loop. I think this should work!

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
NSArray *sortedArray = [myNsArrayController.arrangedObjects sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
for (animal *a in sortedArray)
{
  // Rest of code
}
Firo
  • 15,448
  • 3
  • 54
  • 74
  • thanks! how can i get the name of the nsarraycontroller i added via itnerface builder? (sorry pretty green here.. ) – sharkyenergy Jan 29 '13 at 18:46
  • 1
    No problem, I believe you need to create an IBOutlet for it. So in your .m (or .h) under your interface create an @property here. Name it whatever you would like (say arrayController). Then you should be able to use that instead of myNsArrayController in the above code. That make sense? – Firo Jan 29 '13 at 18:51
  • yes! thanks this is clear now.. but... the "for (animal *a in sortedarray) , should animal be replaced with NSArray? i mean, how can i access now the content of the single rows? thank you! – sharkyenergy Jan 29 '13 at 19:01
  • 1
    No probably not. It would probably be whatever your custom object's name is (or model's name). An example would be a Phone class. It could have three attributes like the one you have (let's say zipcode, number, type). I would then do "for (Phone *curPhone in phoneArray) { curPhone.zipcode }" // this would give me that object's zipcode. Otherwise you could try "for (int i = 0; i < sortedArray.count; i++) { [[sortedArray objectAtIndex:i] animal] }" // this should hopefully give you the animal as a string – Firo Jan 29 '13 at 19:51
  • thanks for your answer, couldnt answer Yesterday because the site was offline! so, if my model is called "Animals" then i would have to write for (Animals *curAnimal in sortedArray) ?? i tried it but it is giving me an error.. "Use of undecleared identifier Animals".. – sharkyenergy Jan 30 '13 at 17:26
  • Mhmm... is it a Animals a class? Could you simply just import Animals.h at the top of your current class? – Firo Jan 30 '13 at 19:49
  • already solved with anotehr question. thanks! i am supposed to use NSManagedObject! thankyou anyway! :) – sharkyenergy Jan 30 '13 at 19:53
1

First sort the array based on animal age.

Then loop through it.

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"animalAge" ascending:YES];
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];

EDIT:

NSArray *yourArray=myNsArrayController.arrangedObjects;
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140