使用描述打印结构
我想知道是否可以使用Cocoa框架中的description
函数来记录struct
的内容。例如:
typedef struct {float a,b,c;}list;
list testlist = {1.0,2.5,3.9};
NSLog(@"%@",testlist); //--> 1.0,2.5,3.9
I would like to know if it is possible to use the description
function in the Cocoa framework to log the contents of a struct
. For example:
typedef struct {float a,b,c;}list;
list testlist = {1.0,2.5,3.9};
NSLog(@"%@",testlist); //--> 1.0,2.5,3.9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以。
description
消息是NSObject
协议中的一个方法,因此根据定义,它必须是一个对象。不过,还有一种更方便的日志调试方法,即使用 LOG_EXPR() 宏。这将采用对象和结构:LOG_EXPR(testlist);
它将输出:
可以找到此代码 这里。
No. The
description
message is a method found in theNSObject
protocol, so by definition, must be an object. There is, however, a more convenient way of log debugging, using aLOG_EXPR()
macro. This will take objects and structs:LOG_EXPR(testlist);
Which would output:
This code can be found here.
description
是一种方法,因此只能在对象上调用。反过来,%@
格式说明符仅适用于响应description
的对象。您可以编写自己的函数,用结构的内容创建一个漂亮的
NSString
:然后在记录该结构时调用该函数:
description
is a method and as such can only be called on an object. In turn, the%@
format specifier only works for objects which respond todescription
.You can write your own function to make a pretty
NSString
with the contents of your struct:Then call that function when you log the struct: