Daniel O'Hara
Daniel O'Hara

Reputation: 13438

Nested NSMutableDictionary efficiency and alternatives

I need to store big amounts of nested strings and access all the data using some simple format (for example, 'my.data.object.path').

For example, if I've got the following structure:

- object1
- object2
-- nested1
-- nested2
--- nestednested1

I need to check, for example, if object2.nested2.nestednested1 exists. Sometimes I also have to check whether object2.nested2 and object2 also exist.

Is it efficient to use nested NSMutableDictionary objects, or should I write some more efficient data storage model from scratch? Maybe there are ready-to-use 3rd-party solutions to this problem that you could suggest.


I was also thinking about storing all my.variable.path paths in a single NSMutableDictionary, so I can set variables simply doing [storage setObject:object forKey:@"my.variable.path"] and extract them by defining a custom method that split incoming path by dot and try to find the shortest chunk of path.

Upvotes: 0

Views: 130

Answers (2)

lnafziger
lnafziger

Reputation: 25740

I would seriously consider using Core Data for this.

  • It works great with large amounts of data.
  • It is very memory efficient, even with very large amounts of data since it loads it as needed.
  • You can access it exactly like in your example.
  • It is built-in mature technology.

The main Apple document covering it is the Core Data Programming Guide.
They also have Core Data Tutorial for iOS which is a little shorter and gets you started.

There are also lots of non-Apple tutorials available on the web. One of my favorites is by Ran Wenderlich: Core Data on iOS 5 Tutorial: Getting Started. He also has other tutorials on Core Data linked from that article and here.

Upvotes: 1

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

Dictionary are hash mapped, it is faster than Arrays.

And what level it is nested you can access it by [dict valueForKeyPath.@"key1.key2...."]

Upto 3 levels it fine, but if you are going beyond that then you can think of designing a Class structure to solve this problem.

Upvotes: 1

Related Questions