How do I disable NSLog?

Cod*_*Guy 18 xcode objective-c

I'd like to do the following in Xcode:

Find all NSLog commands without comments, and replace it with //NSLog...

In other words, I want to comment all NSLog calls. Is this possible? Is there an easy way to do this?

Saq*_*aud 78

等待有更简单的方法.当您不需要NSLOG到常量文件时,只需添加以下行

#define NSLog                       //
Run Code Online (Sandbox Code Playgroud)

并在需要时评论它.

编辑:在最新的Xcode中,您在上述声明中收到错误.所以我想出了最好的方法

#define NSLog(...) 
Run Code Online (Sandbox Code Playgroud)

  • +100这是最好的答案.它应该被接受为这个问题的正确答案. (4认同)
  • 这会产生警告. (3认同)

Mih*_*atu 16

更新:

下面的答案实际上要好得多.看到这里.

初步答案:

你可以做一点点黑客攻击.搜索所有NSLog并替换它们,//NSLog然后再搜索////NSLog并替换它们//NSLog.


Pra*_*rem 12

#define NSLog if(1) NSLog
Run Code Online (Sandbox Code Playgroud)

如果你不希望log set 1为0.

  • 很棒的帮助:) :) (3认同)

Mar*_*wab 10

我必须做一个单独的答案,因为我还不能发表评论,但是当你进行查找和替换时,你真的需要注意的一件事是这样的:

if(BOOL)
NSLog(@"blah blah");

[self doSomething];
Run Code Online (Sandbox Code Playgroud)

如果你注释掉nslog,条件会与下面的方法相关联,如果我错了,请纠正我


Abi*_*ern 7

您的答案对您的问题是正确的.但.您真正的问题是如何在某些条件下转换NSLog.即你希望它们显示为Debug版本,而不是Release版本.在这种情况下尝试定义和使用Cocoa Is My GirlfriendDLog()所描述的宏.如果你正在使用Xcode4,它会更容易,因为和构建定义和取消定义,所以你不必这样做.DebugReleaseDEBUG

这比评论和取消注释代码行要容易得多.