关于Log

func abc()
{
    print(#file)//#file 可以输出当前代码所在的文件
    print(#function)//#function 可以输出当前代码所在的函数
    print(#line)//#line 可以输出当前代码所在的代码行号
}

abc()

自定义Log

func customLog(log:String?,isDebug:Bool = true,file:String = #file,funcName:String = #function,lineNum:Int = #line)
{
    if isDebug == true
    {
        if log != nil
        {
            print(log!,(file as NSString).lastPathComponent,funcName,lineNum)
        }
        else
        {
            print("要输出的内容格式或类型非法")
        }
    }

}

customLog(log: "hello world",isDebug: false)

由于Swift没有宏定义,可以做如下操作

func customLog(log:String?,file:String = #file,funcName:String = #function,lineNum:Int = #line)
{
    #if DEBUG
    if log != nil
        {
            print(log!,(file as NSString).lastPathComponent,funcName,lineNum)
        }
        else
        {
            print("要输出的内容格式或类型非法")
        }
    #endif
}

customLog(log: "hello world",isDebug: false)

results matching ""

    No results matching ""