Remove println() for release version iOS Swift

前端 未结 19 1382
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 15:55

I would like to globally ignore all println() calls in my Swift code if I am not in a Debug build. I can\'t find any robust step by step instructions for this a

相关标签:
19条回答
  • 2020-11-29 16:50

    I ended up using this:

    #if DEBUG
    func dLog(_ item: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, _ line: Int = #line) {
        print("\(Date()) [\((file as NSString).lastPathComponent):\(line) \(function)] \(item())")
    }
    #else
    func dLog(_ item: @autoclosure () -> Any) {}
    #endif
    

    It is quite compact, prints some useful informations (timestamp, swift file name, code line, function name) and at least in my tests I could not find any logged strings in the application binary file when opened in hex editior.

    0 讨论(0)
  • 2020-11-29 16:51

    Here is a function that I use, which works perfectly in Swift 3:

    func gLog<T>( _ object: @autoclosure() -> T, _ file: String = #file, _ function: String = #function, _ line: Int = #line)
        {
        #if DEBUG
            let value = object()
            let stringRepresentation: String
    
            if let value = value as? CustomDebugStringConvertible
                {
                stringRepresentation = value.debugDescription
                }
            else if let value = value as? CustomStringConvertible
                {
                stringRepresentation = value.description
                }
            else
                {
                fatalError("gLog only works for values that conform to CustomDebugStringConvertible or CustomStringConvertible")
                }
    
            let fileURL = NSURL(string: file)?.lastPathComponent ?? "Unknown file"
            let queue = Thread.isMainThread ? "UI" : "BG"
        let gFormatter = DateFormatter()
        gFormatter.dateFormat = "HH:mm:ss:SSS"
            let timestamp = gFormatter.string(from: Date())
    
            print("✅ \(timestamp) {\(queue)} \(fileURL) > \(function)[\(line)]: " + stringRepresentation + "\n")
        #endif
        }
    

    Here is an example of the output it generates:

    Explanation:

    • the green checkmark is used to enable you to quickly see your print (gLog) messages in the console, where they can sometimes get lost in a sea of other messages

    • the time/date stamp

    • the thread it is being run on -- in my case it is either the MainThread (which I call UI), or not the MainThread (which I call BG, for background thread)

    • the name of the file that the gLog message resides in

    • the function within the file that the gLog message resides in

    • the line number of the gLog message

    • the actual gLog message you would like to print out

    Hope this is useful to someone else!

    0 讨论(0)
  • 2020-11-29 16:51

    You could define debug_println whose contents would be roughly:

    #if DEBUG
      println()
    #endif
    
    0 讨论(0)
  • 2020-11-29 16:53

    XCode 8 introduced a few new build settings.
    In particular one referred to Active Compilation Conditions does in a similar way what Other Flags settings did.

    "Active Compilation Conditions" is a new build setting for passing conditional compilation flags to the Swift compiler.

    As per XCode 8 (tested in 8.3.2) you will get this by default:

    So without any config you can write the following:

    #if DEBUG
        print("⚠️ Something weird happened")
    #endif
    

    I strongly recommend you that if you use this approach extensively create a class/struct/function that wraps this logging logic. You may want to extend this further down the road.

    0 讨论(0)
  • 2020-11-29 16:55

    My Project was developed in Objective C, but from the past year I have started merging new code in Swift, So In Swift below solution worked for me, I have added that code in My Swift constant file :

    func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
        #if DEBUG
        items.forEach {
            Swift.print($0, separator: separator, terminator: terminator)
        }
        #endif
    }
    
    0 讨论(0)
  • 2020-11-29 16:58

    My Solution is use this code in AppDelegate before class

    // Disable console log in live app
    #if !arch(x86_64) && !arch(i386)
        public func debugPrint(items: Any..., separator: String = " ", terminator: String = "\n") {
    
        }
        public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
    
        }
    #endif
    
    class AppDelegate: UIResponder, UIApplicationDelegate {
    // App Delegate Code 
    
    }
    
    0 讨论(0)
提交回复
热议问题