thatidiotguy thatidiotguy - 5 months ago 107x
Swift Question

Disabling NSLog For Production In Swift Project

So this answer Do I need to disable NSLog before release Application? gives a great way to disable NSLog in a production environment, but unfortunately, this solution does not seem to work for Swift projects. My approach was to place the following code in the bridging header .h file that I am using for some pods in my project.

#ifdef DEBUG
#define DLog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
#define DLog(...) do { } while (0)

However, using DLog in Swift code is causing the compiler to state that they are unrecongnized symbols. Is there somewhere else I should be placing this
or is there a different solution for Swift project in general?


You'll need to set up a compiler flag to use the Swift preprocessor - go to the Swift Compiler - Custom Flags section of Build Settings to set up a -D DEBUG flag:

DEBUG flag

Then in your code you can define a DLog() function and only print your message if the DEBUG flag is set:

func DLog(message: String, function: String = __FUNCTION__) {
    #if DEBUG
    println("\(function): \(message)")