Is there a Swift alternative for NSLog(@“%s”, __PRETTY_FUNCTION__)

前端 未结 11 1127
梦谈多话
梦谈多话 2020-12-02 07:39

In Objective C you can log the method that is being called using:

NSLog(@\"%s\", __PRETTY_FUNCTION__)

Usually this is used from a logging m

11条回答
  •  离开以前
    2020-12-02 08:25

    Swift 3.x+

    If you don't want the entire file name then here's a quick fix for that.

    func trace(fileName:String = #file, lineNumber:Int = #line, functionName:String = #function) -> Void {
        print("filename: \(fileName.components(separatedBy: "/").last!) function: \(functionName) line: #\(lineNumber)")
    }
    
    filename: ViewController.swift function: viewDidLoad() line: #42
    

提交回复
热议问题