如何将对象的属性打印到 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:39:45  来源:igfitidea点击:

How do I print an object's property to the Debugger Console in Xcode?

xcodedebugging

提问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 titleproperty 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.titlein the Expressions window, I get the error:

当我myObject.title在“表达式”窗口中尝试时,出现错误:

out of scope

超出范围

... even though myObjectin 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.

这很快就会让人厌烦,但工作正常。