publish: true
aliases:
- Debugging in Swift| Mechanism | Production | Testing | Debugging |
|---|---|---|---|
| fatalError() in Swift | Terminates the app unconditionally | Terminates the test | Terminates the app unconditionally |
| assertions in Swift | Completely ignored, no effect | Causes test failure if condition fails | Terminates the app if condition fails |
Logger's .error(_:) |
Logs error message without terminating | Logs error message without failing tests | Logs error message without terminating |
Logger's .info(_:) |
Logs informational message | Logs informational message | Logs informational message |
| `reportIssue()` by pointfree.co | Shows unobtrusive warnings by default (customizable) | Becomes a test failure | Shows purple runtime warnings in Xcode |
While debugging, it can be easy to get overloaded with irrelevant and poorly formatted logs in the console. Here are some strategies that can help.
Annotate your types with the @DebugDescription macro (after Swift 6).[1]
@DebugDescription
struct WatchLaterItem {
let video: Video
let name: String
let addedOn: Date
var debugDescription: String {
"\ (name) - \(addedOn)"
}
}