views:

121

answers:

3

In a situation where I need to save all data members in a NSDictionary, does it make more sense to put structs (custom types, or even scalars i.e. CGPoint) in my own wrapper (not NSValue), so I can avoid the overhead of encoding/decoding it every time I get or set the member?

For large structs (16 floats) the savings is IMO significant. But even with a CGPoint I'd be saving 4 bytes of copying plus encoding/decoding time.

+1  A: 

Well, NSDictionary types require objects as their members anyway, so you can't store a struct in an NSDictionary. You'd have to wrap it in a container object.

jer
Yes, but my choice is between NSValue and a simple container object.
hyn
+1  A: 

Why not just measure it for your specific case? That is the only really reliable way to find it out.

If both options are not sufficient you could look into CFDictionary with pointers to plain structs by setting the value callbacks accordingly or take a C++ std::map/std::tr1::unordered_map for a spin (if you don't mind mixing in C++ that is).

Georg Fritzsche
CFDictionary may be the answer.
hyn
+1  A: 

You might actually go a step further: Abandon the struct entirely and make it a model object. You can then integrate logic (e.g., computed properties) into it and give it the ability to encode and decode itself in any relevant formats, as well as put it into collections.

Peter Hosey