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?
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 !=
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.
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.
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.
#function
might be what you want.
print(#function)
class Log {
func info(_ info: String, funcName: String = #function) {}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With