There are a few reasons it’s a good idea to print to the console only while developing. The most important of these is security. Very often projects will print JSON requests and responses for debug purposes. This logging may contain passwords, API keys or other sensitive information. A malicious actor who was intent on it, could make their way into low levels of the app and pull information that was logged.
Adding the custom print statement below globally to your project (i.e. top level, not in a class or struct, and in a file available to all targets) will make it so printing will only occur if the app is in a debug build configuration (see how to set up build configurations).
func print(_ items: Any...) {
#if DEBUG
for item in items {
Swift.print("\(item)", separator: "", terminator: "")
}
Swift.print("")
#endif
}
You can now use print()
throughout your project like you normally would. One caveat is you won’t be able to pass in a custom a separator or terminator to your print statements. I’ve never had to do that in all my iOS development, though.