swift中的print()vs debugPrint()

时间:2017-01-24 11:08:48

标签: swift swift3 swift3.0.2

这可能是一个简单的问题,但由于swift中print()和debug()打印之间的清晰理解,我无法理解在哪里使用每一个。

5 个答案:

答案 0 :(得分:40)

如果需要有关正在打印到控制台的内容的更多信息,请使用debugPrint。附加信息通常对调试很有用。

print() - 将给定项目的文本表示写入标准输出。

debugPrint() - 将最适合调试的给定项目的文本表示写入标准输出。

基本上,debugPrint会添加对调试类型信息等有用的其他信息。

一个例子:

[
  {
    "request": {
      "uri": "http://something",
      "headers": [
        {
          "Content-Type": "application/json"
        }
      ],
      "body": ".. snip .. you get the idea",
      "other-stuff": ""
    },
    "response": {
      // something along the lines of the request structure to be filled in after the request completed
    }
  },
  // second request/response here, and so on.
]

答案 1 :(得分:18)

使用print()是一种常规方式,可以看到您正在创建的内容。它没有表现出“无关紧要”。表示打印变量不必要的信息。

e.g。

print("test")
// prints: test

然而,使用debugPrint()会将推断类型添加到输出中。

e.g。

debugPrint("test")
// prints: "test"

请注意它如何添加引号以告知您它是一个字符串。

Erica Sadun创造了这两个功能如何不同的完美范例: Swift: Logging

答案 2 :(得分:8)

文章链接:print-vs-debugprint

如果您拨打网络电话并执行debugPrint(response)而不是print(response),您将获得更多有价值的信息。 请参阅以下示例代码:

示例代码使用iTunes搜索功能

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

控制台输出(删除部分响应字段)

打印

SUCCESS: {
    resultCount = 1;
    results =     (
                {
            artistId = 909253;
            artistName = "Jack Johnson";
            artistViewUrl = "https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
        }
    );
}

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
}

答案 3 :(得分:1)

如果您同时实现(defn square-function [input] (is (= 0 1)) (* input input)) (hystrix/defcommand square {} [data] (square-function data)) (deftest testing-square (is (= 16 (square 4)))) CustomDebugStringConvertible协议,则CustomStringConvertible方法默认使用debugPrint的内容,而debugDescription方法默认使用print内容。

答案 4 :(得分:0)

debugPrint() 将最适合调试的给定项的文本表示写入标准输出,它由几个参数组成:

func debugPrint(_ items: Any..., separator: String = " ", terminator: String = "\n")

项目: 可以包含零个或多个要打印的项目。

separator:要在每个项目之间打印的字符串。默认为单个空格 (" ")。

终止符:打印所有项目后要打印的字符串。默认为换行符(“\n”)。

为了便于理解,我在下面写了一些例子:

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"

要打印由非空格分隔的项目,请传递一个字符串作为分隔符。

debugPrint(1.0, 2.0, 3.0, 4.0, 5.0, separator: " ... ")
// Prints "1.0 ... 2.0 ... 3.0 ... 4.0 ... 5.0"

每次调用 debugPrint(_:separator:terminator:) 的输出默认包含一个换行符。要打印没有尾随换行符的项目,请传递一个空字符串作为终止符或传递您想要的任何其他内容。

for n in 1...5 {
    debugPrint(n, terminator: "")
}
// Prints "12345"