Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seeing the value of a synthesized property in the Xcode debugger when there is no backing variable

I just recently began using synthesized instance variables in my iPhone projects. The problem is, I can't see the synthesized ivars in the debugger. Is there any way to view the properties of an object in the debugger when it's not using the explicitly declared instance variables?


I need to clarify the second question. I'm not asking about how to access properties, or what they do; I know all that stuff. I was under the impression that you could not access instance variables directly when using synthesized ivars based on this post. I've clearly been able to do what I previously thought wasn't possible. I'm wondering what's going on.

I'm using Xcode 3.2.4/iPhone Simulator/LLVM Compiler 1.5.

like image 281
kubi Avatar asked Jul 17 '10 04:07

kubi


People also ask

How do I see variables in Xcode?

See Variable Values in Code and the Variable Viewer When your app pauses at a breakpoint, hover over a variable in your source code to view its current value. If the variable is an image or other type that isn't expressible as text, click the Quick Look button at the upper-right to see a preview of the variable.

How do you evaluate expressions in Xcode?

Use the po command in the Debug area Set up a breakpoint on the relevant area of code, then when the program stops at the breakpoint, enter commands in the Console in the Debug Area. The relevant command is po (print object) followed by the expression you want to evaluate.

How do I print a variable in Xcode?

Print a variableClick the Add Action button and select Debugger Command. Put any print command in a debug console in a text field. Make sure you prefix it with po , e.g., po print(view) . Check Automatically continue after evaluating actions.

How do I debug my code in Xcode?

To set up your breakpoint, you simply need to open a project in Xcode or Android Studio, and click close to the code line number. That's how it looks in Xcode. You'll see a blue arrow appear, indicating that the execution will pause when it reaches that point.


2 Answers

Edited to add answer to second part:

This works on Xcode 3.1 so I don't see why it won't work on later versions

What you could do is send messages directly to the object from the console while debugging.

Presumably you've stopped at a breakpoint and you're looking at the variables in the debug view. for objects, these show you the pointers. You may not see the iVar, but you have the pointer to the object and you can send it messages. for example:

  • You've stopped at some breakpoint within the object
  • The variable view shows the pointer address of self to be (say) 0x1031380.
  • In the console type po [0x1031380 title] (note that there is no semicolon) and enter
  • You should see what you want in the console.

When you declare a property with (retain) and subsequently synthesize the property, you're creating setters that retain the object/value passed to them. so in your case above you should rewrite the method as:

- (void)viewDidLoad {
    self.title = @"woah";
}

And the string will be retained as part of the setter. Also, I prefer to use (copy) for class clusters that have mutable/immutable pairs (NSString, NSSet, NSArray, etc). That way, the property can't be changed externally.

like image 153
Abizern Avatar answered Sep 29 '22 23:09

Abizern


You dont have to use the pointer/address of the variable. Instead you can use the variable name like this:

po [myVar title]

xcode will help you to type the variable name (myVar above) if the variable is in the scope.

like image 31
Udgita Omkara Avatar answered Sep 29 '22 22:09

Udgita Omkara