views:

49

answers:

4

I have a dictionary .plist that has a subarray in it, however during the conversion in Json in converts to an array. The item is "profile" in the image below, as you can see, it's a Dictionary, how do I turn programmatically convert into an array":

alt text

I would also like the items in the dictionary to have Item 0, Item 1, etc. so it looks like this when completed so I can use it in a drill down UItable.

alt text

Thanks for the feedback on the question, I reworked it.

Michael

A: 

Given that the dictionary contains a series of key/value pairs, it isn't clear what turning it into an array even means. As well, you haven't said what you want to do with the array.

Do you want just the values? Do you need them in key,value,key,value order? Do they need to be in some order? Do you want the array to replace the dictionary? Do you need the array by itself?


Given the extent of the rewriting you are doing, I would suggest creating a container -- NSMutableDictionary or NSMutableArray, as appropriate -- and then walking down the existing container, inserting new items in your mutable container in whatever form you need.

It would be straightforward using for (... in ...) to enumerate the dictionaries and arrays.

As long as you continue to use the string values found within the original -- there is no reason to copy them -- then the memory use won't be horribly egregious.

Relevant documentation:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Collections/Collections.html#//apple_ref/doc/uid/10000034i

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html#//apple_ref/doc/uid/10000048i

bbum
reworked question.
Michael Robinson
I'm a newbie, can you point me in the right direction for learning about enumeration.Thanks for your time.
Michael Robinson
This is great..Obviously I need to do this reading. How do I out a check mark on two answers?
Michael Robinson
A: 

Click the "Dictionary" value in the the Type column and change it to "Array".

Dave DeLong
I added "programmatically".
Michael Robinson
A: 

To discard the keys of a dictionary and just get the values, use allValues.

drawnonward
Order will be unspecified, though.
bbum
A: 

Assuming that you initialize items as a NSMutableArray, the following would be the easiest way to handle this:

NSDictionary *profile;
NSArray *pArray;
for (NSMutableDictionary *anItem in items) {
    profile=[anItem valueForKey:@"profile"];
    pArray=[NSArray arrayWithObjects:[profile valueForKey:@"car"],[profile valueForKey:@"color"],[profile valueForKey:@"make"],nil];
    [anItem setValue:pArray forKey:@"profile"];
}

This will ensure the order of the created array is always the same.

I have to say, however, that I think you will very seldom encounter a situation where you have to convert a nested dictionary to an array. You certainly don't need to do so for a tableview.

TechZen
It was my understanding that you need the "Item 0", "Item 1" format in the profile so that I can push to a new view controller, All the examples I see have this format to push the view. Am I wrong?
Michael Robinson
Now you don't need that to display a tableview. You don't need an array at all. The "Item 0", "Item 1" is just the way the plist editor displays arrays so that you can see the elements in order. They don't appear in the actual operation array. For tableviews you need ordered data and usually you create an array for that but there is no requirement that you store the data that way. You can create arrays representing table rows on the fly if you like. That is how Core Data handles it.
TechZen
Okay , I'll have to look into this...every single example of a drill down or UItableview uses a dictionary or array with the "Item 0, Item 1" formatting. without those, My drilldowns dont load. I'll do some reading. Thanks.
Michael Robinson