swift中的print()与debugPrint()

swift中的print()与debugPrint(),swift,methods,printing,Swift,Methods,Printing,这可能是一个简单的问题,但由于对swift中print()和debug()print的理解很清楚,我无法理解在何处使用它们 使用print()是一种常规方法,可以直观地查看您正在创建的内容。它不会显示表示打印变量不必要的“无关”信息 e、 g 但是,使用debugPrint()将推断的类型添加到输出中 e、 g 注意它是如何添加引号以让您知道它是一个字符串的 Erica Sadun创建了一个完美的例子来说明这两种功能的不同: 当您需要有关正在打印到控制台的内容的更多信息时,可以使用debugPr

这可能是一个简单的问题,但由于对swift中print()和debug()print的理解很清楚,我无法理解在何处使用它们

使用
print()
是一种常规方法,可以直观地查看您正在创建的内容。它不会显示表示打印变量不必要的“无关”信息

e、 g

但是,使用
debugPrint()
将推断的类型添加到输出中

e、 g

注意它是如何添加引号以让您知道它是一个字符串的

Erica Sadun创建了一个完美的例子来说明这两种功能的不同:

当您需要有关正在打印到控制台的内容的更多信息时,可以使用debugPrint。附加信息通常对调试有用

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

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

基本上debugPrint添加了对调试有用的附加信息,如类型信息等。

例如:

print(1...5)
// Prints "1...5"


debugPrint(1...5)
// Prints "CountableClosedRange(1...5)"
文章链接:

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

示例代码:使用iTunes搜索Api

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

对于打印

SUCCESS: {
    resultCount = 1;
    results =     (
                {
            artistId = 909253;
            artistName = "Jack Johnson";
            artistViewUrl = "https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
        }
    );
}
[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
}
对于debugPrint

SUCCESS: {
    resultCount = 1;
    results =     (
                {
            artistId = 909253;
            artistName = "Jack Johnson";
            artistViewUrl = "https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
        }
    );
}
[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
}
[请求]:获取https://itunes.apple.com/search?term=jack+强生极限=1
[答复]:{URL:https://itunes.apple.com/search?term=jack+johnson&limit=1}{状态代码:200,标题{
“访问控制允许来源”=“*”;
“缓存控制”=“最大使用年限=86345”;
连接=“保持活动”;
“内容处置”=“附件;文件名=1.txt”;
“内容长度”=1783;
“内容类型”=“文本/javascript;字符集=utf-8”;
日期=“2017年9月23日星期六14:29:11 GMT”;
“严格的交通安全”=“最大年龄=31536000”;
Vary=“接受编码”;
“X-Apple-Partner”=“origin.0”;
“X-Cache”=“TCP_未命中a23-76-156-143.deploy.akamaitechnologies.com(AkamaiGHost/9.1.0.4-20866905)();
“X-Cache-Remote”=“TCP_未命中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_;”;
“苹果始发系统”=MZStoreServices;
“苹果序列”=0;
“苹果计时应用程序”=“86毫秒”;
“苹果tk”=假;
“x-apple-application-instance”=1000492;
“x-apple-application-site”=NWK;
“x-apple-kingle-correlation-key”=VEF3J3UWCHKUSGPHDZRI6RB2QY;
“x-apple-orig-url”=”https://itunes.apple.com/search?term=jack+约翰逊极限=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;
} }
[数据]:1783字节
[结果]:成功:{
结果计数=1;
结果=(
{
artistId=909253;
艺人名称=“杰克·约翰逊”;
artistViewUrl=”https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
}
);
}
[时间表]:时间表:
{
“请求开始时间”:527869893.013,
“初始响应时间”:527869893.033,
“请求完成时间”:527869893.034,
“序列化完成时间”:527869893.035,
“延迟”:0.020秒,
“请求持续时间”:0.021秒,
“序列化持续时间”:0.001秒,
“总持续时间”:0.021秒
}

如果您都实现了
CustomDebugStringConvertible
CustomStringConvertible
协议,那么
debugPrint
方法默认使用
debugDescription
内容,而
print
方法默认使用
描述
内容。

func debugPrint(_ items: Any..., separator: String = " ", terminator: String = "\n")
项目: 可以包含零个或多个要打印的项目

分隔符:每个项目之间要打印的字符串。默认值为单个空格(“”)

终止符:打印所有项目后要打印的字符串。默认值为换行符(“\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(quo;分隔符:终止符:)的输出都包含一个换行符。要打印不带尾随换行符的项目,请传递空字符串作为终止符,或传递任何其他所需内容

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

另外,debugPrint按原样打印\n\t之类的内容,而不格式化文本。因此,如果您想看到漂亮的json,例如,您应该使用print。
for n in 1...5 {
    debugPrint(n, terminator: "")
}
// Prints "12345"