Xcode Debugger: view value of variable

前端 未结 7 774
囚心锁ツ
囚心锁ツ 2020-12-12 14:45

My code in a UITableViewController:

delegate.myData = [myData objectAtIndex:indexPath.row];

How can I see the values of delegate.myDa

相关标签:
7条回答
  • 2020-12-12 15:12

    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]
    
    0 讨论(0)
  • 2020-12-12 15:17

    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.

    0 讨论(0)
  • 2020-12-12 15:17

    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);
    0 讨论(0)
  • 2020-12-12 15:18

    Try Run->Show->Expressions

    Enter in the name of the array or whatever you're looking for.

    0 讨论(0)
  • 2020-12-12 15:22

    Also you can:

    1. Set a breakpoint to pause the execution.
    2. The object must be inside the execution scope
    3. Move the mouse pointer over the object or variable
    4. A yellow tooltip will appear
    5. Move the mouse over the tooltip
    6. Click over the two little arrows pointing up and down
    7. A context menu will pop up
    8. Select "Print Description", it will execute a [object description]
    9. The description will appear in the console's output

    IMHO a little bit hidden and cumbersome...

    0 讨论(0)
  • 2020-12-12 15:22

    You can print values onto console window at run-time. Below are steps :

    1. Place a break-point for which you want to get values
    2. Now perform step-by-step debug.
    3. Place a cursor on variable/delegate whose value is to be checked at run-time.
    4. Now this will show description of variable/delegate
    5. Clicking on "i" will show detailed description
    6. This will also print details onto console window.

    Screenshot for printing details on console window

    0 讨论(0)
提交回复
热议问题