Disabling NSLog For Production In Swift Project

前端 未结 8 2103
遥遥无期
遥遥无期 2020-12-23 10:26

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

8条回答
  •  被撕碎了的回忆
    2020-12-23 10:39

    Pure magic _isDebugAssertConfiguration() does all stuff instead of the Custom Flags-related mess:

    func log(argument: T, file: String = #file, line: Int = #line, function: String = #function) {
        guard _isDebugAssertConfiguration() else {
            return
        }
    
        let fileName = NSURL(fileURLWithPath: file, isDirectory: false).URLByDeletingPathExtension?.lastPathComponent ?? "Unknown"
    
        print("\(fileName)@\(line)/\(function): \(argument)")
    }
    

    See more info (and options) about it here: https://stackoverflow.com/a/34532569/496389 .

提交回复
热议问题