NSDictionary to NSArray

 

Usually I write in spanish, but since this is a universal issue (at least for the people who code in objective-c) I’m going to post this “answer” in english.

I was struggling for a couple of hours with something that in the end was very simple, trying to put the values and keys of a NSDictionary in a NSArray to order them, I’m a N00b, yes. But that’s how we all started, so hopefully in some months I will be less N00bish. I was trying at first with this code:

 NSArray *array = [dictionary valueForKey:@"Root"];

But when I was iterating the array

for (int i=0;i<=[array count];i++){
   NSDictionary *item = [array objectAtIndex:i];
}

The debugger send me a error message telling me that I was in fact wrong, and the NSDictionary can’t use the objectAtIndex. Wait, what? but it’s an array!!!  Well, I was telling the NSArray at the beginning to work as a NSDictionary and I was unaware.

What I found out was that I needed to send all the values instead of using the valueForKey. And that was it. Solution served, easy indeed.

NSArray *array = [dictionary allValues];
 

Sofia Swidarowicz

I'm an iOS Software Engineer mostly. Known as phynet in the internez. I'm me, full of memory failure and lovely karma.

 

Leave a Reply

Your email address will not be published. Required fields are marked *