views:

66

answers:

2

Hi,

In xcode you can use po object to see a textual representation of a given object. Is it possible to convert from this textual representation to a real objective c object?

Thanks

+4  A: 

I suppose you could parse out the address, assign it to a pointer, and retrieve the object from memory that way, but that IS A HORRIBLY BAD IDEA AND YOU SHOULD NEVER DO THAT.

Real question: what are you trying to do?

Dave DeLong
Yeah... what Dave said. Do not **ever** write code that relies upon the string returned from the `-description` method. Down that path lies madness.
bbum
truer.. but in my case I want to create a fake object that retuns the same thing as the real object. Without the real object doing anywork. Essentially serialize the real object... deserialize to a new object. I want to deserialze to an object in a test run not on actual production code.
A: 

No, the representation you see from po <instance> is the result of -[<instance> debugDescription], which by default returns -[<instance> description], as described in http://developer.apple.com/mac/library/technotes/tn2004/tn2124.html.

Unless the instance you're dealing with just happens to provide a description which is a serialized form of itself, you're SOL. Most objects don't.

The real question, as Dave points out is, what are you trying to do? po only works in the gdb console, so I assume this is a debugging issue. In that case, do you know that the gdb console supports sending messages to instances? So you can do:

po [<myInstance> methodReturningAnOtherObject]

or

po [<myInstance> valueForKeyPath:@"some.long.key.path"]

to follow the object graph from within the debugger console.

Barry Wark