This might be a simple question but because of clear understanding between print() and debug() print in swift I am unable to understand where to use each one.
5 Answers
You use debugPrint when you want more information about what is being printed to the console. The additional information is usually useful for debugging.
print() - Writes the textual representations of the given items into the standard output.
debugPrint() - Writes the textual representations of the given items most suitable for debugging into the standard output.
Basically debugPrint adds additional information that is useful for debugging like type information etc.
An example:
print(1...5)
// Prints "1...5"
debugPrint(1...5)
// Prints "CountableClosedRange(1...5)"
-
3Also debugPrint prints things like \n \t as is, without formatting the text. So if you want to see pretty json for example, you should use print.– RealNmaeMay 30, 2018 at 8:00
Using print()
is a regular way to visualy see what you are creating. It does not show 'irrelevant' information that is not neccesary to represent the printed variable.
e.g.
print("test")
// prints: test
Using debugPrint()
however adds the inferred type to the output.
e.g.
debugPrint("test")
// prints: "test"
Note how it adds the quotation marks to let you know it is a string.
Erica Sadun has created a perfect example of how these two functions differ: Swift: Logging
Article link : print-vs-debugprint
If You make a network call and do a debugPrint(response)
instead of print(response)
, you will get a lot more valuable information.
See the below example code:
Sample Code : Using iTunes Search Api
let urlReq = URLRequest(url: URL(string: "https://itunes.apple.com/search?term=jack+johnson&limit=1")!)
Alamofire.request(urlReq).responseJSON { (data) in
print(data)
print("\n\n\n\n\n\n\n\n\n")
debugPrint(data)
}
Console Output (Removing some of the response fields)
For print
SUCCESS: {
resultCount = 1;
results = (
{
artistId = 909253;
artistName = "Jack Johnson";
artistViewUrl = "https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
}
);
}
For debugPrint
[Request]: GET https://itunes.apple.com/search?term=jack+johnson&limit=1
[Response]: <NSHTTPURLResponse: 0x610000223860> { URL: https://itunes.apple.com/search?term=jack+johnson&limit=1 } { status code: 200, headers {
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "max-age=86345";
Connection = "keep-alive";
"Content-Disposition" = "attachment; filename=1.txt";
"Content-Length" = 1783;
"Content-Type" = "text/javascript; charset=utf-8";
Date = "Sat, 23 Sep 2017 14:29:11 GMT";
"Strict-Transport-Security" = "max-age=31536000";
Vary = "Accept-Encoding";
"X-Apple-Partner" = "origin.0";
"X-Cache" = "TCP_MISS from a23-76-156-143.deploy.akamaitechnologies.com (AkamaiGHost/9.1.0.4-20866905) (-)";
"X-Cache-Remote" = "TCP_MISS from a23-45-232-92.deploy.akamaitechnologies.com (AkamaiGHost/9.1.0.4-20866905) (-)";
"X-True-Cache-Key" = "/L/itunes.apple.com/search ci2=limit=1&term=jack+johnson__";
"apple-originating-system" = MZStoreServices;
"apple-seq" = 0;
"apple-timing-app" = "86 ms";
"apple-tk" = false;
"x-apple-application-instance" = 1000492;
"x-apple-application-site" = NWK;
"x-apple-jingle-correlation-key" = VEF3J3UWCHKUSGPHDZRI6RB2QY;
"x-apple-orig-url" = "https://itunes.apple.com/search?term=jack+johnson&limit=1";
"x-apple-request-uuid" = "a90bb4ee-9611-d549-19e7-1e628f443a86";
"x-apple-translated-wo-url" = "/WebObjects/MZStoreServices.woa/ws/wsSearch?term=jack+johnson&limit=1&urlDesc=";
"x-content-type-options" = nosniff;
"x-webobjects-loadaverage" = 0;
} }
[Data]: 1783 bytes
[Result]: SUCCESS: {
resultCount = 1;
results = (
{
artistId = 909253;
artistName = "Jack Johnson";
artistViewUrl = "https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
}
);
}
[Timeline]: Timeline:
{
"Request Start Time": 527869893.013,
"Initial Response Time": 527869893.033,
"Request Completed Time": 527869893.034,
"Serialization Completed Time": 527869893.035,
"Latency": 0.020secs,
"Request Duration": 0.021secs,
"Serialization Duration": 0.001secs,
"Total Duration": 0.021secs
}
if you both implementation CustomDebugStringConvertible
and CustomStringConvertible
protocol, then debugPrint
method default use debugDescription
content, and print
method default use description
content.
debugPrint() Writes the textual representations of the given items most suitable for debugging into the standard output and it consists of several parameters:
func debugPrint(_ items: Any..., separator: String = " ", terminator: String = "\n")
items: can include zero or more item to print.
separator: A string to print between each item. The default is a single space (" ").
terminator: The string to print after all items have been printed. The default is a newline ("\n").
I have written some examples below for more understanding:
debugPrint("One two three four five")
// Prints "One two three four five"
debugPrint(1...5)
// Prints "ClosedRange(1...5)"
debugPrint(1.0, 2.0, 3.0, 4.0, 5.0)
// Prints "1.0 2.0 3.0 4.0 5.0"
To print the items separated by something other than a space, pass a string as separator.
debugPrint(1.0, 2.0, 3.0, 4.0, 5.0, separator: " ... ")
// Prints "1.0 ... 2.0 ... 3.0 ... 4.0 ... 5.0"
The output from each call to debugPrint(_:separator:terminator:) includes a newline by default. To print the items without a trailing newline, pass an empty string as terminator or pass any other things you want.
for n in 1...5 {
debugPrint(n, terminator: "")
}
// Prints "12345"