views:

632

answers:

4

I am running through an iPhone development tutorial and I have a weird bug that I am investigating using the debugger. I have the following code that checks to see if a object is in bounds. I am trying to see what the value of ball.center.x is at a certain point, but because center is a property accessor selector, I don't get the value when I hover over it in the Xcode debugger.

if (ball.center.x > self.view.bounds.size.width || ball.center.x < 0) {
    ballVelocity.x = -ballVelocity.x;
}

Is there a way to do this? I think I must just be missing something. I suppose I could update the code to assign the value to a variable that I could then watch in the debugger, but that seems like a sloppy work around to a common problem.

Thanks!

+2  A: 

I'll assume center is an CGPoint.

In addition to the Xcode GUI debugger, there's also a gdb prompt available when you're debugging. Try this:

p* ball

or

p ball.center
nall
p* ball did enumerate all the properties for the ball instance, but still did not display the center CGPoint property. p ball.center gave an error: "there is no member named center". I ended up going with the NSLog suggestion. However, I did not know about GDB, so this was a helpful suggestion. I need to read more about it, but it seems like a valuable tool.
jslatts
gdb is good stuff. If you post a bit more of your code (specifically the types of these things) I'll tell you how to access it in gdb.
nall
Is gdb aware of property getters? If not, that might be the issue. You could try invoking the getter yourself: `po [ball center]`.
outis
`po`, if you don't know, prints a representation of an object by sending `-description` (or is it `-debugDescription`?) to the object and printing the result.
outis
I believe it's -debugDescription if it exists, falling back to -description if not.
nall
+2  A: 

and then there's also the venerable quick and dirty:

if (ball.center.x > self.view.bounds.size.width || ball.center.x < 0) {
    NSLog(@"ball center: %d",ball.center.x);
    ballVelocity.x = -ballVelocity.x;
}
pxl
I ended up using the log code NSLog(@"ball center Y: %f", ball.center.y); to get the values. Thanks for the tip. It seems there is no shortcut for viewing these values in the debugger, but logging works easily enough.
jslatts
sometimes quick and dirty does the job
pxl
A: 

Try this gdb command:

p (CGPoint)[ball center] 

I use this trick with UIView.frame, it works.

A: 

There is even easier way to do this than usingthe gdb console. From main menu choose:

Run -> Variables View -> View in expression window

And in the expression window's text field type:

(CGPoint)[ball center]

The main problem with expression windows showing the 'out of scope' error message is it almost never knows the return type of a function (or method), so it does not know how to present the returned variable. Apple gives us a clue what to do is such cases here under the 'Tips on using the Expressions window'.

b005t3r