How do I disable NSLog?

前端 未结 15 2098
陌清茗
陌清茗 2020-12-05 07:46

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

相关标签:
15条回答
  • 2020-12-05 08:28

    Add the following line to your .pch file

    #define NSLog
    

    for enable comment it

    0 讨论(0)
  • 2020-12-05 08:31

    You can do this in a single find and replace operation. You can just do this simple Regular Expression replace. This handles both the commented(//) and non-commented lines. This also works even if the previous commented lines has more than two forward slashes(like ///)instead of rwo. You can refer this link. Do the following steps.

    1. Select Edit > Find > Find and Replace in Workspace
    2. Style => Regular Expression
    3. Type (/)*(NSLog.*) in Find field.
    4. Do the find operation.
    5. Type //\2 in the Replace field.
    6. Do the replace operation.

    Enjoy the beauty of regular expressions ;-)

    0 讨论(0)
  • 2020-12-05 08:34

    try this also:

        #define NSLog(@"YOUR MESSAGE HERE!") do { } while (0)
    
    0 讨论(0)
  • 2020-12-05 08:35

    The answers you have are correct for your question. But. Your real question is how to turn of NSLogs in certain conditions. i.e. you want them to show for Debug builds, and not for Release builds. In which case try defining and using the DLog() macro as described on Cocoa Is My Girlfriend. If you're using Xcode4 it's even easier because the Debug and Release builds define and undefine DEBUG so you don't have to do that.

    It's a lot easier than commenting and uncommenting lines of code.

    0 讨论(0)
  • 2020-12-05 08:36
    #ifdef RELEASE
       #define NSLog(...) do { } while (0)
    #endif
    

    is the best way i found so far.

    0 讨论(0)
  • 2020-12-05 08:36

    I would do this

    #define EnableNSLog 1
    
    #if EnableNSLog == 0
    #define NSLog   //
    #elif EnableNSLog == 1
    #warning Disable NSLog
    #endif
    

    This should generate a warning message to remind me to disable NSLog before final release.

    0 讨论(0)
提交回复
热议问题