Swift: determine what object called a function?

前端 未结 4 1754
感情败类
感情败类 2020-12-09 01:33

I am writing some Swift code and I would like to know the class of the object that called the function. I don\'t want to pass in any parameters. From within the function I w

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 02:28

    You can use following template to know from which file, line number in file, and function this someFunction is called:

    func someFunction(file: String = #file, line: Int = #line, function: String = #function)
    {
        NSLog("\(file.lastPathComponent):\(line) : \(function)")
    }
    

提交回复
热议问题