如何编写一个可以带参数的宏?
我想使用一些 NSAssert 和其他东西来在我的应用程序中更好地进行调试。 NSAssert 需要一个在断言失败时打印的字符串。不错,但除非您在该字符串中输入一大堆信息,否则毫无用处,如果到处输入这些信息,可能会变得一团糟。
所以我想制作一个宏,它将使用一个充满有用信息(如类名、方法名和其他内容)的字符串来执行 NSAssert 调用。但更重要的是,我还想提供自定义评论。想象一下这样的宏:
USEFUL_ASSERT(foo != nil, @"that isn't good, real")
由于很多原因,我不能在这里使用函数或方法调用,因为上下文会丢失,我不能找出哪个类导致了该问题,哪个方法导致了该问题。这只能在该方法本身内部完成,因此我必须为这些信息传递很多参数,例如 [self class] 和 _cmd,但我不希望所有这些东西散落各处。插入的代码越短,维护起来就越好。
有什么想法吗?
I want to use some NSAssert stuff and other things to enable better debugging in my app. NSAssert wants a string which it prints if the assertion fails. Nice, but useless unless you type a whole bunch of information in that string, which can become a big mess when done all over the place.
So I want to make a macro that will do the NSAssert call with an string equipped full of useful information like the class name, the method name and other stuff. But more important, I want to provide a custom comment as well. Imagine a macro like this:
USEFUL_ASSERT(foo != nil, @"that wasn't good, really")
for a lot of reasons I can't use a function or method call here, because context would be lost and I could not find out which class caused that problem, which method caused that problem. That can only be done inside that method itself, so I would have to pass a lot of parameters for this information like [self class] and _cmd, but I don't want all this stuff scattered everywhere. The shorter the code to insert, the better to maintain.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看宏文档。
Take a look at the macro docs.