How to print out the method name and line number in swift

后端 未结 4 1350
礼貌的吻别
礼貌的吻别 2020-12-12 18:26

Here is an example of what I want to do:

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)
{
            


        
4条回答
  •  攒了一身酷
    2020-12-12 18:31

    You can use #function, #file, #line

    Here is the implementation of log method in swift : https://github.com/InderKumarRathore/SwiftLog

    Below is the snippet

    public func debugLog(object: Any, functionName: String = #function, fileName: String = #file, lineNumber: Int = #line) {
      #if DEBUG
        let className = (fileName as NSString).lastPathComponent
        print("<\(className)> \(functionName) [#\(lineNumber)]| \(object)\n")
      #endif
    }
    

提交回复
热议问题