Tutorial :NSdictionary object in plist needs to be a array of arrays, how to convert?



Question:

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 http://www.baublet.com/images/dict2.png

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 http://www.baublet.com/images/array4.png

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

Michael


Solution:1

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


Solution:2

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


Solution:3

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


Solution:4

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »