Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 395 Vote(s) - 3.37 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print() vs debugPrint() in swift

#1
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.
Reply

#2
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"
```
Reply

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

#4

Article link : [print-vs-debugprint][1]

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

[To see links please register here]

[Response]: <NSHTTPURLResponse: 0x610000223860> { URL:

[To see links please register here]

} { 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
}


[1]:

[To see links please register here]

Reply

#5
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()][1] - Writes the textual representations of the given items into the standard output.

[debugPrint()][2] - 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)"




[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#6
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][1]


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through