print() vs debugPrint() in swift

32,774

Solution 1

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)"

Solution 2

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

Solution 3

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
}

Solution 4

if you both implementation CustomDebugStringConvertible and CustomStringConvertible protocol, then debugPrint method default use debugDescription content, and print method default use description content.

Share:
32,774
Rajan Twanabashu
Author by

Rajan Twanabashu

It's been about 7 years I am working as Mobile application developing for iOS and android platform. In recent time I consider my self as stack developer because in my professional career I have not only worked in mobile application development, I have been actively participating in other aspect of Software product such as server side programming with java, php, good experience in using AWS for cloud solution, designing and testing and lot more. In summary I consider my self as a candidate who can choose right platform and tools to develop software for mobile specially for iOS.

Updated on July 06, 2022

Comments

  • Rajan Twanabashu
    Rajan Twanabashu almost 2 years

    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.

  • RealNmae
    RealNmae about 6 years
    Also 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.