斯威夫特:print() vs println() vs NSLog() [英] Swift: print() vs println() vs NSLog()

查看:16
本文介绍了斯威夫特:print() vs println() vs NSLog()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

printNSLogprintln 有什么区别,什么时候应该使用它们?

What's the difference between print, NSLog and println and when should I use each?

例如,在 Python 中,如果我想打印字典,我只需 print myDict,但现在我有 2 个其他选项.我应该如何以及何时使用它们?

For example, in Python if I wanted to print a dictionary, I'd just print myDict, but now I have 2 other options. How and when should I use each?

推荐答案

几个不同:

  1. print vs println:

print 函数在调试应用程序时在 Xcode 控制台中打印消息.

The print function prints messages in the Xcode console when debugging apps.

println 是它的一个变体,它在 Swift 2 中被删除并且不再使用.如果您看到使用 println 的旧代码,您现在可以安全地将其替换为 print.

The println is a variation of this that was removed in Swift 2 and is not used any more. If you see old code that is using println, you can now safely replace it with print.

在 Swift 1.x 中,print 没有在打印字符串的末尾添加换行符,而 println 可以.但是现在,print 总是在字符串的末尾添加换行符,如果您不想这样做,请提供 terminator 参数".

Back in Swift 1.x, print did not add newline characters at the end of the printed string, whereas println did. But nowadays, print always adds the newline character at the end of the string, and if you don't want it to do that, supply a terminator parameter of "".

NSLog:

  • NSLog 将时间戳和标识符添加到输出中,而 print 不会;

  • NSLog adds a timestamp and identifier to the output, whereas print will not;

NSLog 语句同时出现在设备的控制台和调试器的控制台中,而 print 只出现在调试器的控制台中.

NSLog statements appear in both the device’s console and debugger’s console whereas print only appears in the debugger console.

NSLog 使用 printf 样式的格式字符串,例如

NSLog in iOS 10-13/macOS 10.12-10.x uses printf-style format strings, e.g.

  NSLog("%0.4f", CGFloat.pi)

这将产生:

2017-06-09 11:57:55.642328-0700 MyApp[28937:1751492] 3.1416

2017-06-09 11:57:55.642328-0700 MyApp[28937:1751492] 3.1416

iOS 14/macOS 11 中的

  • NSLog 可以使用字符串插值.(然后,在 iOS 14 和 macOS 11 中,我们通常更喜欢 Logger 而不是 NSLog.请参阅下一点.)

  • NSLog from iOS 14/macOS 11 can use string interpolation. (Then, again, in iOS 14 and macOS 11, we would generally favor Logger over NSLog. See next point.)

    现在,虽然 NSLog 仍然有效,但我们通常会使用统一日志记录"(见下文)而不是 NSLog.

    Nowadays, while NSLog still works, we would generally use "unified logging" (see below) rather than NSLog.

    有效的 iOS 14/macOS 11,我们有 Logger 接口到统一日志"系统.有关 Logger 的介绍,请参阅 WWDC 2020 探索 Swift 中的日志记录.

    Effective iOS 14/macOS 11, we have Logger interface to the "unified logging" system. For an introduction to Logger, see WWDC 2020 Explore logging in Swift.

    • 要使用Logger,必须导入os:

    import os
    

  • NSLog一样,统一日志也会同时向Xcode调试控制台和设备控制台输出消息

  • Like NSLog, unified logging will output messages to both the Xcode debugging console and the device console, too

    创建一个Loggerlog一条消息给它:

    Create a Logger and log a message to it:

    let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "network")
    logger.log("url = (url)")
    

    当您通过外部 Console 应用观察应用时,您可以根据 subsystemcategory 进行过滤.将您的调试消息与 (a) 其他子系统代表您的应用生成的消息或 (b) 来自其他类别或类型的消息区分开来非常有用.

    When you observe the app via the external Console app, you can filter on the basis of the subsystem and category. It is very useful to differentiate your debugging messages from (a) those generated by other subsystems on behalf of your app, or (b) messages from other categories or types.

    您可以指定不同类型的日志消息,.info.debug.error.故障.critical.notice.trace等:

    You can specify different types of logging messages, either .info, .debug, .error, .fault, .critical, .notice, .trace, etc.:

    logger.error("web service did not respond (error.localizedDescription)")
    

    因此,如果使用外部控制台应用程序,您可以选择仅查看某些类别的消息(例如,如果您在控制台操作"菜单上选择包含调试消息",则仅显示调试消息).这些设置还规定了许多关于事物是否记录到磁盘的微妙问题细节.有关详细信息,请参阅 WWDC 视频.

    So, if using the external Console app, you can choose to only see messages of certain categories (e.g. only show debugging messages if you choose "Include Debug Messages" on the Console "Action" menu). These settings also dictate many subtle issues details about whether things are logged to disk or not. See WWDC video for more details.

    默认情况下,非数字数据会在日志中进行编辑.在您记录 URL 的示例中,如果应用程序是从设备本身调用的,并且您正在从 macOS 控制台应用程序观看,您将在 macOS 控制台中看到以下内容:

    By default, non-numeric data is redacted in the logs. In the example where you logged the URL, if the app were invoked from the device itself and you were watching from your macOS Console app, you would see the following in the macOS Console:

    url = <私人>

    url = <private>

    如果您确信此消息不会包含用户机密数据,并且您希望在 macOS 控制台中查看字符串,则必须执行以下操作:

    If you are confident that this message will not include user confidential data and you wanted to see the strings in your macOS console, you would have to do:

    os_log("url = (url, privacy: .public)")
    

  • 在 iOS 14/macOS 11 之前,iOS 10/macOS 10.12 引入了用于统一日志记录"的 os_log.有关统一日志记录的一般介绍,请参阅 WWDC 2016 视频 统一日志记录和活动跟踪.

    Prior to iOS 14/macOS 11, iOS 10/macOS 10.12 introduced os_log for "unified logging". For an introduction to unified logging in general, see WWDC 2016 video Unified Logging and Activity Tracing.

    • 导入os.log:

    import os.log
    

  • 你应该定义subsystemcategory:

    let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "network")
    

    使用 os_log 时,您将使用 printf 样式的模式而不是字符串插值:

    When using os_log, you would use a printf-style pattern rather than string interpolation:

    os_log("url = %@", log: log, url.absoluteString)
    

  • 您可以指定不同类型的日志消息,.info.debug.error.故障(或.default):

    os_log("web service did not respond", type: .error)
    

  • 使用 os_log 时不能使用字符串插值.例如,使用 printLogger 你可以:

  • You cannot use string interpolation when using os_log. For example with print and Logger you do:

    logger.log("url = (url)")
    

    但是使用 os_log,您必须这样做:

    But with os_log, you would have to do:

    os_log("url = %@", url.absoluteString)
    

  • os_log 强制执行相同的数据隐私,但您在 printf 格式化程序中指定公共可见性(例如 %{public}@ 而不是 %@).例如,如果您想从外部设备查看它,您必须这样做:

  • The os_log enforces the same data privacy, but you specify the public visibility in the printf formatter (e.g. %{public}@ rather than %@). E.g., if you wanted to see it from an external device, you'd have to do:

    os_log("url = %{public}@", url.absoluteString)
    

  • 如果您想查看 Instruments 的活动范围,也可以使用兴趣点"日志:

  • You can also use the "Points of Interest" log if you want to watch ranges of activities from Instruments:

    let pointsOfInterest = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: .pointsOfInterest)
    

    然后开始一个范围:

    os_signpost(.begin, log: pointsOfInterest, name: "Network request")
    

    最后以:

    os_signpost(.end, log: pointsOfInterest, name: "Network request")
    

    有关详细信息,请参阅 https://stackoverflow.com/a/39416673/1271826.

    For more information, see https://stackoverflow.com/a/39416673/1271826.

    底线,print 足以使用 Xcode 进行简单的日志记录,但统一的日志记录(无论是 Logger 还是 os_log)实现了相同的目的,但提供更强大的功能.

    Bottom line, print is sufficient for simple logging with Xcode, but unified logging (whether Logger or os_log) achieves the same thing but offers far greater capabilities.

    在调试必须在 Xcode 之外进行测试的 iOS 应用程序时,统一日志记录的威力会得到显着缓解.例如,在测试后台 iOS 应用程序进程(如后台获取)时,连接到 Xcode 调试器 会更改应用程序生命周期.因此,您经常需要在物理设备上进行测试,从设备本身运行应用程序,而不是从 Xcode 的调试器启动应用程序.统一日志记录让您仍然可以从 macOS 控制台应用程序查看您的 iOS 设备日志语句.

    The power of unified logging comes into stark relief when debugging iOS apps that have to be tested outside of Xcode. For example, when testing background iOS app processes like background fetch, being connected to the Xcode debugger changes the app lifecycle. So, you frequently will want to test on a physical device, running the app from the device itself, not starting the app from Xcode’s debugger. Unified logging lets you still watch your iOS device log statements from the macOS Console app.

    这篇关于斯威夫特:print() vs println() vs NSLog()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆