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 n
I am a pretty junior-level developer and need things defined a little more clearly to follow along at times. I had been having trouble with a similar issue, replacing println() instead, and when i asked that question my inquiry was marked as a duplicate of this question.
After lots of research, trial, error, error, error, and ERROR!!, I found that the sequence I needed to follow is:
-D DEBUG
and press Return to commit the line.Add a new Swift file to your project. You are going to want to make a custom class for the file, so enter text along the lines of the following:
class Log {
var intFor : Int
init() {
intFor = 42
}
func DLog(message: String, function: String = __FUNCTION__) {
#if DEBUG
println("\(function): \(message)")
#endif
}
}
(I was having trouble getting the class init to be accepted by Xcode today, so the init may be a bit more heavyweight than necessary.)
Now you will need to reference your custom class in any class in which you intend to use the new custom function in place of println()
Add this as a property in every applicable class:
let logFor = Log()
Now you can replace any instances of println()
with logFor.DLog()
. The output also includes the name of the function in which the line was called.
println()
is also a bit more flexible with the input, so I couldn't use this in every instance in my code.Below, I have modified the answer by Nate Cook given above. This version works with both String
and NSString
arguments:
func DLog<T>(message:T, function: String = __FUNCTION__) {
#if DEBUG
if let text = message as? String {
print("\(function): \(text)")
}
#endif
}
It can be handy when you need to do something like this:
DLog("Received data:")
DLog(NSString(data:httpResponseBodyData, encoding:NSUTF8StringEncoding))