上QQ阅读APP看书,第一时间看更新
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 a String of the name of the function in which the literal is used
- #file, which is converted into a String containing the path to the file in which the literal is used, starting with:
/Users/...
- #line and #column are converted to an Int 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: applicationDidFinishLaunching
File: /Users/.../Documents/Books/myBooks/Packt/macOS
Programming/Content/ch03/myApp/myApp/AppDelegate.swift
Line: 41
Column: 26
The special literals are useful not only in your code, but also in breakpoints, which we'll look at now.