What is the best method to build an NSDictionary from an NSArray of flattened paths? For example, I want to convert this array’s contents:
<array>
<string>packs/</string>
<string>packs/Children/</string>
<string>packs/Children/Letters</string>
<string>packs/Children/Letters/abc.pack</string>
<string>packs/Children/Numbers</string>
<string>packs/Children/Numbers/123.pack</string>
<string>packs/Children/Numbers/10_2_30.pack</string>
<string>packs/General/</string>
</array>
…into an NSDictionary of the path segments and filenames, like so:
packs/
Children/
Letters/
abc.pack
Numbers/
123.pack
10_20_30.pack
General/
Would it be best to first look for the array items with a file extension (.pack) and build the structure back from that point? Or try to build the structure line by line through the array’s contents?
Any help is greatly appreciated!
I will assume that all leaf nodes end with
.packand all branch nodes do not, for simplicity.A dictionary is a set of key/value pairs. It’s not clear what you want the value for key
abc.packto be in thepacks/Lettersdictionary. I’ll just use the string@"leaf node!"as the value.You can do this pretty easily with a helper function that inserts a path into the dictionary tree.
Then it’s just a matter of creating an initial, empty tree (
NSMutableDictionary) and inserting each path into it: