Converting an NSValue back to the struct type that was stored in it?
Asked Answered
U

2

11

I'm storing Chipmunk Physics' cpShape objects in an NSMutableDictionary by way of NSValue objects and the following lines of code:

NSValue *shapeValue = [[NSValue alloc] initWithBytes: shape objCType: @encode(cpShape)];
[staticBodiesInUse setObject: shapeValue forKey: name];

I now need to get the cpShape back out, to compare it to another shape. How can I do that? I see a getValue: method in NSValue but it needs a buffer, not too sure what to do with it.

Urbanize answered 4/2, 2013 at 11:34 Comment(0)
D
15

So the answer by trojanfoe is only partly correct.

There is a huge problem with doing that. When you create the NSValue that way, you are copying the cpShape struct, and getting it back out, you are copying it again. cpShape structs are pretty much exclusively used by reference. Each time you copy it, you get a new reference to the new copy, and some of those copies exist on the stack and get destroyed silently and automatically. Very very bad.

Instead you want to create a NSValue using [NSValue valueWithPointer:shape] and get that pointer back using [value pointerValue]. This way the NSValue is only storing a pointer to the original cpShape.

Dew answered 5/2, 2013 at 1:29 Comment(2)
And using a cpShape * is problem free? I don't think so and will lead to insidious bugs unless used very carefully.Ringtail
@Dew can you elaborate a bit more on why creating a copy of the struct in the current stack frame is bad (or worse than passing around pointers to the struct)? Or is that due to the way chipmunk operates?Earthshine
R
8

Isn't it simply:

NSValue *value = [staticBodiesInUse objectForKey:name];
cpShape shape;
[value getValue:&shape];
Ringtail answered 4/2, 2013 at 11:42 Comment(1)
It would appear so, yes. I missed the class on referencing, clearly ;)Urbanize

© 2022 - 2024 — McMap. All rights reserved.