print() vs debugPrint() in swift

♀尐吖头ヾ 提交于 2020-12-24 03:51:38

问题


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.


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



回答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




回答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
}



回答4:


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



来源:https://stackoverflow.com/questions/41826683/print-vs-debugprint-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!