Swift中的日志行号和方法名[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我正在将Objective C代码转换为Swift。找到这段代码:

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

我知道这段代码的作用但是我无法转换它。我发现this很棒的链接,但这只有Objective C参考。

ios swift nslog
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.