3

Is there anyway to delete the NSLog lines from the app by any trick/tool? I usually use NSLog's in each and every method to understand the flow of control and to know about the values of the app's variables. I also use lots of comment lines to explain the nature of methods and variables.

At some point these NSLogs and comment lines make the program hard to for me to understand. So I need to keep deleting and recreating them. Is there a way to show/hide them by any trick in Xcode?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Confused
  • 3,846
  • 7
  • 45
  • 72

2 Answers2

15

Use the global research & replace tool (cmd-shift-f, or Edit, find, Find in Workspace)

Clic on Find, select Replace
Style => Regular expression

For the NSLogs, search

NSLog\(.*\).*$

and replace by a space.

For the comments, search

\/\/.*$

and

\/\*.*\*\/

and replace by a space.

And finish by replacing manually those ones

/* fjeizghrij
   eopgfjeipgez
*/

because I don't know how to grab them safely?

EDIT :

At last, beware of this global replace because you won't be able to undo ! Copy/paste your project before, for example. You should use the preview fonctionality of the global replace too, and check each entry.

Martin
  • 11,881
  • 6
  • 64
  • 110
  • 1
    Close to a down vote.... Using regular expressions to modify code is remarkably fragile. That'll do the wrong thing if there is, as is common, a sub-expression on the log line. And it permanently loses the logs. – bbum Aug 01 '12 at 11:45
  • Yes, youre right, but it make the job. Maybe I should have talk about the preview fonctionality of global replace. – Martin Aug 01 '12 at 13:12
  • 2
    ...and upvote for the edit! Thanks for the clarification. Global find-and-destroy is very useful, but it is important to know how destructive it is. Could be useful to cut a release branch, find and destroy all debugging goop, then build for submission! – bbum Aug 01 '12 at 14:04
  • Hi just a suggestion do not Replace without checking code i did it once and one guy had boobytrapped the code with if like if() nslog.It lead to if being applied to next line of code. – amar Mar 03 '16 at 09:46
4

I'm not sure what the exact reason is why you want to remove the NSLog lines and comments.

if you can read the source code hard, to remove the comments, set the comments colour in the Xcode preferences to same as the background or set their font size to 1 and you won't see them when you read the code. :)

I have no idea for the NSLog, but I'm using the following way to avoid the unwanted logging in the final release.

this is a simple macro:

#ifdef DEBUG
#define DebugLog(...) NSLog(__VA_ARGS__)
#else
#define DebugLog(...) { }
#endif

I'm using the DebugLog(...) as I would use the NSLog(...) normally, and the Xcode is logging only in DEBUG mode, I don't need to remove any log when I create the release version of the app.

holex
  • 23,961
  • 7
  • 62
  • 76