Подтвердить что ты не робот

Как распечатать имя метода и номер строки в быстром

Вот пример того, что я хочу сделать:

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)
{
    let  nm =  NetworkModel()
    nm.sendlog("file name :AppDelegate , line number : 288", info: " Failed to register: \(error)")
}

текущий сценарий я сделал это жестко закодированный line number значения и file name. но возможно ли программно выбрать line number и file name.

4b9b3361

Ответ 1

Literal        Type     Value

#file          String   The name of the file in which it appears.
#line          Int      The line number on which it appears.
#column        Int      The column number in which it begins.
#function      String   The name of the declaration in which it appears.
#dsohandle     String   The dso handle.

пример

print("Function: \(#function), line: \(#line)") 

Со значениями по умолчанию в параметрах вы также можете создать функцию

public func track(_ message: String, file: String = #file, function: String = #function, line: Int = #line ) { 
    print("\(message) called from \(function) \(file):\(line)") 
}

который можно использовать как это

track("enters app")

В Swift 2.1

 Literal        Type     Value

__FILE__       String   The name of the file in which it appears.
__LINE__       Int      The line number on which it appears.
__COLUMN__     Int      The column number in which it begins.
__FUNCTION__   String   The name of the declaration in which it appears.

для получения дополнительной информации см. документацию

Ответ 2

Вы можете использовать #function, #file, #line

Вот реализация метода журнала в swift: https://github.com/InderKumarRathore/SwiftLog

Ниже приведен фрагмент

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
}

Ответ 3

static func DLog(message: String, file: String = #file, function: String = #function, line: Int = #line, column: Int = #column) {
    print("\(file) : \(function) : \(line) : \(column) - \(message)")
}

Ответ 4

Для быстрых 3 и быстрых 4:

func printLog(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
    #if DEVELOPMENT
        let className = file.components(separatedBy: "/").last
        print(" ❌ Error ----> File: \(className ?? ""), Function: \(function), Line: \(line), Message: \(message)")
    #endif
}

// "❌ Error ----> File: classNameViewController.swift, function: functionName(), Line: 123, Message: messageError"