Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get function name in Swift

I am looking to get the name of function in swift.

I basically want something like

__FUNCTION__

but from a different function e.g.

func do () {
   code etc
}

func doStuff () {
   var funcName = do().name
}

the above would be perfect.

Any ideas?

like image 484
RyanCosans Avatar asked Jan 24 '16 02:01

RyanCosans


People also ask

How do you name a function in Swift?

In Swift 3 the right keyword is #function. You could pass a pointer to a String object which is used to store the function name. For example: func myFunction(param1:Any, param2:Any, functionName: UnsafeMutablePointer<String>?) -> Any { if functionName !=

How do you print a function in Swift?

Swift 2.2 and earlier You have a few options: print(__FUNCTION__) will output functionName() if the function has no arguments. print(__FUNCTION__) will output functionName (without parentheses) if the function has one or more arguments.

How do you return a function in Swift?

To do this, write a dash then a right angle bracket after your function's parameter list, then tell Swift what kind of data will be returned. Inside your function, you use the return keyword to send a value back if you have one.

What is Inout in Swift?

Swift inout parameter is a parameter that can be changed inside the function where it's passed into. To accept inout parameters, use the inout keyword in front of an argument. To pass a variable as an inout parameter, use the & operator in front of the parameter.


1 Answers

#function might be what you want.

print(#function)

class Log {
    func info(_ info: String, funcName: String = #function) {}
}
like image 112
DawnSong Avatar answered Oct 07 '22 17:10

DawnSong