How to disable NSLog all over the app?

心已入冬 提交于 2019-12-18 05:47:32

问题


I want to disable NSLog() across all instances in an app. I found some code that does that:

#ifndef DEBUG
#define NSLog //
#endif

But adding this code to each file isn't good idea. How can I make it easier?


回答1:


Xcode has a precompiled header file ({project-name}-Prefix.pch in the Supporting Files group by default) that is a great place to put code that will be used across every file in the project. For going a step further and improving the log message itself, see Is it true that one should not use NSLog() on production code?.




回答2:


Add the following lines to your constants file:

#define DEBUGGING NO    //or YES
#define NSLog if(DEBUGGING)NSLog

The two following sentences give the same results

#define NSLog

and

#define NSLog //

because all text that begin with // are deleted in a precompiling phase ("//" included)




回答3:


What I do is put this in the precompiled header file (YourAppName.pch):

#define MyLog if(0); else NSLog

Then I change all NSLog to MyLog everywhere else in the app. It works as if it were NSLog. But when you want to disable NSLog, go back to the .pch file and change the 0 to 1 and presto, all logging stops.




回答4:


This will also take care of the warnings that arise when using

#define NSLog //

The code:

#ifndef DEBUG
  #define NSLog(...)
#endif



回答5:


Just add the following lines when you do'"t need NSLOG to your constants file

#define NSLog                       //

and comment it when you need it.



来源:https://stackoverflow.com/questions/16002001/how-to-disable-nslog-all-over-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!