代码之家  ›  专栏  ›  技术社区  ›  Nitish

Swift记录行号和方法名称[副本]

  •  0
  • Nitish  · 技术社区  · 6 年前

    我正在转换一个 Objective C 编码到 Swift . 找到这个代码:

    #define MPLog(fmt, ...) \
        do { \
            if ([MPPush getLoggingPref] && ![MPPush getEnvironmentPref]) { \
                NSLog(fmt, ##__VA_ARGS__); \
            } \
        } while(0)  
    

    this 目标C 参考文献。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Cris    6 年前

    Swift提供了一些表达式,以便记录行号和方法名称等:

    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.
    

    例如:

    func logFunctionName(string: String = #function) {
        print(string)
    }
    func myFunction() {
        logFunctionName() // Prints "myFunction()".
    }
    

    official documentation