如何将对象的属性打印到 Xcode 中的调试器控制台?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3548700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I print an object's property to the Debugger Console in Xcode?
提问by Senseful
I have an object that was created with the Core Data code generator:
我有一个使用 Core Data 代码生成器创建的对象:
.h file:
.h 文件:
@interface MyObject : NSManagedObject
{
}
@property (nonatomic, retain) NSString * title;
@end
.m file:
.m 文件:
@implementation MyObject
@dynamic title;
@end
I put a breakpoint and now I want to print the title
property of one of its instances (myObject
) to the console.
我放置了一个断点,现在我想将title
其中一个实例 ( myObject
)的属性打印到控制台。
When I try po myObject.title
, I get the error:
当我尝试时po myObject.title
,出现错误:
There is no member named title.
没有名为 title 的成员。
When I try po [myObject title]
, I get the error:
当我尝试时po [myObject title]
,出现错误:
Target does not respond to this message selector.
Target 不响应此消息选择器。
When I try myObject.title
in the Expressions window, I get the error:
当我myObject.title
在“表达式”窗口中尝试时,出现错误:
out of scope
超出范围
... even though myObject
in the same window allows me to see some of its members.
...即使myObject
在同一个窗口中允许我看到它的一些成员。
How can I print an object's property to the console and/or Expressions window in Xcode?
如何将对象的属性打印到 Xcode 中的控制台和/或表达式窗口?
回答by Jonathan del Strother
You can get around this by using valueForKey -
你可以通过使用 valueForKey 来解决这个问题 -
po [myObject valueForKey:@"title"]
which gets tiresome pretty quickly, but works ok.
这很快就会让人厌烦,但工作正常。