xcode 格式化字符串输出的整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/763774/
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
Formatting integer for string output
提问by 4thSpace
In a breakpoint action, I'm going this:
在断点操作中,我将执行以下操作:
@(const char *)[(NSString*)[myobject retainCount]UTF8String]@
which gives this output:
这给出了这个输出:
<Error: Cannot access memory at address 0x2>
If I do this:
如果我这样做:
@(NSString*)[myboject retainCount]@
it outputs a memory address. But if I do this in code:
它输出一个内存地址。但是如果我在代码中这样做:
NSLog(@"retain count is: %d", [myobject retainCount]);
it gives this output:
它给出了这个输出:
2009-04-18 09:58:48.085 myapp[80277:20b] retain count is: 1
What is the syntax needed to output correctly in the breakpoint action?
在断点操作中正确输出所需的语法是什么?
Also, where can I find a complete listing of the format keys for breakpoint actions?
另外,在哪里可以找到断点操作的格式键的完整列表?
回答by Sean Patrick Murphy
You should be able to actually use a "Debugger Command" breakpoint action over the "Log" action, setting the command text to:
您应该能够在“日志”操作上实际使用“调试器命令”断点操作,将命令文本设置为:
p (int)[myObject retainCount]
p (int)[myObject retainCount]
If you want to log the description of an Objective-C object:
如果你想记录一个 Objective-C 对象的描述:
po myObject
po myObject
You can use a Log action before it to display a message indicating what is being printed if you want.
如果需要,您可以在它之前使用 Log 操作来显示一条消息,指示正在打印的内容。
As for the specific retain count issue you're logging in this case, examining retain counts directly and trying to solve memory related bugs that way isn't considered a great practice. See this postfor a good explanation about that.
至于您在这种情况下记录的特定保留计数问题,直接检查保留计数并尝试以这种方式解决与内存相关的错误不被认为是一种很好的做法。请参阅这篇文章以获得关于此的很好的解释。
回答by Ed Marty
retainCount returns a number. Simply casting it to a string is incorrect, since it's a number, not a string. To print a number as a string you have to either call:
保留计数返回一个数字。简单地将它转换为字符串是不正确的,因为它是一个数字,而不是一个字符串。要将数字打印为字符串,您必须调用:
printf("%d",[myobject retaincount]);
or print out this string:
或打印出这个字符串:
[NSString stringWithFormat:@"%d",[myobject retaincount]];