Debugging Swift code can involve a lot of print statements, logging, and guesswork, especially when you’re tracking down issues that only happen intermittently. Thankfully, Swift gives us a set of powerful compiler-magic identifiers that make it easy to know where something is happening in your code.
Three of the most useful are:
#file
#function
#line
Here's how you can use them within a function:
func myFunction(
file: String = #file,
line: Int = #line,
function: String = #function
) {
let callerFunctionName = function.components(separatedBy: "(").first ?? function
print("ℹ️ Debug Info")
print("ℹ️ Called from function: \(callerFunctionName)")
print("ℹ️ Line: \(line)")
print("ℹ️ File: \(file)")
}
Hopefully you find this Swift tip helpful, I use this little snippet all the time.