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

感情迁移 提交于 2019-12-10 02:06:21

问题


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.


回答1:


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.




回答2:


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.




回答3:


If you don't want to manually type po [blahblah], you can do what xnav suggested here, where you either explicitly declare you instance variables in the header, or "in the debug variable area right click on 'self' and select "Add Expression", then enter e.g.' _wordLength' and the ivar will be displayed"



来源:https://stackoverflow.com/questions/3270248/seeing-the-value-of-a-synthesized-property-in-the-xcode-debugger-when-there-is-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!