Logging with special literals
Among the reserved keywords of Swift are a small number of what Apple calls special literals. These are keywords that are substituted at compile time, and four of them are very useful for debugging purposes. They are as follows:
#function
, which is converted into aString
of the name of the function in which the literal is used#file
, which is converted into aString
containing the path to the file in which the literal is used, starting with:
/Users/...
#line
and#column
are converted to anInt
of the line and column number at which they appear
For any kind of ad-hoc debugging, or custom debugging classes, these are an essential tool. Add the following code to the AppDelegate.swift
file's applicationDidFinishLaunching
method (or anywhere else, for that matter):
print("Function: \(#function)") print("File: \(#file)") print("Line: \(#line)") print("Column: \(#column)")
The output from this code will look something like the following:
Function...