Is there a Swift alternative for NSLog(@“%s”, __PRETTY_FUNCTION__)

前端 未结 11 1155
梦谈多话
梦谈多话 2020-12-02 07:39

In Objective C you can log the method that is being called using:

NSLog(@\"%s\", __PRETTY_FUNCTION__)

Usually this is used from a logging m

11条回答
  •  无人及你
    2020-12-02 08:41

    Starting from Swift 2.2 we should use:

    • #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.

    From The Swift Programming Language (Swift 3.1) at page 894.

    func specialLiterals() {
        print("#file literal from file: \(#file)")
        print("#function literal from function: \(#function)")
        print("#line: \(#line) -> #column: \(#column)")
    }
    // Output:
    // #file literal from file: My.playground
    // #function literal from function: specialLiterals()
    // #line: 10 -> #column: 42
    

提交回复
热议问题