Xcode Debugger: view value of variable
My code in a UITableViewController:
delegate.myData = [myData objectAtIndex:indexPath.row];
How can I see the values of delegate.myData
or indexPath.row
in the Debugger? delegate.myData
should be an array and indexPath.row
an int
. I can only see memory 开发者_如何学JAVAaddresses of the objects delegate
and indexPath
but where are myData
and row
?
Check this How to view contents of NSDictionary variable in Xcode debugger?
I also use
po variableName
print variableName
in Console.
In your case it is possible to execute
print [myData objectAtIndex:indexPath.row]
or
po [myData objectAtIndex:indexPath.row]
I agree with other posters that Xcode as a developing environment should include an easy way to debug variables. Well, good news, there IS one!
After searching and not finding a simple answer/tutorial on how to debug variables in Xcode I went to explore with Xcode itself and found this (at least for me) very useful discovery.
How to easily debug your variables in Xcode 4.6.3
In the main screen of Xcode make sure to see the bottom Debug Area by clicking the upper-right corner button showed in the screenshot.
Now set a Breakpoint – the line in your code where you want your program to pause, by clicking the border of your Code Area.
Now in the Debug Area look for this buttons and click the one in the middle. You will notice your area is now divided in two.
Now run your application.
When the first Breakpoint is reached during the execution of your program you will see on the left side all your variables available at that breakpoint.
You can expand the left arrows on the variable for a greater detail. And even use the search field to isolate that variable you want and see it change on real time as you "Step into" the scope of the Breakpoint.
On the right side of your Debug Area you can send to print the variables as you desire using the mouse's right-button click over the desired variable.
As you can see, that contextual menu is full of very interesting debugging options. Such as Watch that has been already suggested with typed commands or even Edit Value… that changes the runtime value of your variable!
Also you can:
- Set a breakpoint to pause the execution.
- The object must be inside the execution scope
- Move the mouse pointer over the object or variable
- A yellow tooltip will appear
- Move the mouse over the tooltip
- Click over the two little arrows pointing up and down
- A context menu will pop up
- Select "Print Description", it will execute a [object description]
- The description will appear in the console's output
IMHO a little bit hidden and cumbersome...
Your confusion stems from the fact that declared properties are not (necessarily named the same as) (instance) variables.
The expresion
indexPath.row
is equivalent to
[indexPath row]
and the assignment
delegate.myData = [myData objectAtIndex:indexPath.row];
is equivalent to
[delegate setMyData:[myData objectAtIndex:[indexPath row]]];
assuming standard naming for synthesised properties.
Furthermore, delegate
is probably declared as being of type id<SomeProtocol>
, i.e., the compiler hasn’t been able to provide actual type information for delegate
at that point, and the debugger is relying on information provided at compile-time. Since id
is a generic type, there’s no compile-time information about the instance variables in delegate
.
Those are the reasons why you don’t see myData
or row
as variables.
If you want to inspect the result of sending -row
or -myData
, you can use commands p
or po
:
p (NSInteger)[indexPath row]
po [delegate myData]
or use the expressions window (for instance, if you know your delegate
is of actual type MyClass *
, you can add an expression (MyClass *)delegate
, or right-click delegate
, choose View Value as…
and type the actual type of delegate
(e.g. MyClass *
).
That being said, I agree that the debugger could be more helpful:
There could be an option to tell the debugger window to use run-time type information instead of compile-time information. It'd slow down the debugger, granted, but would provide useful information;
Declared properties could be shown up in a group called properties and allow for (optional) inspection directly in the debugger window. This would also slow down the debugger because of the need to send a message/execute a method in order to get information, but would provide useful information, too.
You can print values onto console window at run-time. Below are steps :
- Place a break-point for which you want to get values
- Now perform step-by-step debug.
- Place a cursor on variable/delegate whose value is to be checked at run-time.
- Now this will show description of variable/delegate
- Clicking on "i" will show detailed description
- This will also print details onto console window.
This gets a little complicated. These objects are custom classes or structs, and looking inside them is not as easy on Xcode as in other development environments.
If I were you, I'd NSLog the values you want to see, with some description.
i.e:
NSLog(@"Description of object & time: %i", indexPath.row);
Try Run->Show->Expressions
Enter in the name of the array or whatever you're looking for.
Right-click in the Debug area, which is below the Code Editor, and choose the Add Expression... context menu item. Enter the variable here, e.g. delegate.myData
and that's it.
精彩评论