首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Swift: print() vs println() vs NSLog()

Swift: print() vs println() vs NSLog()
EN

Stack Overflow用户
提问于 2014-09-21 00:59:14
回答 6查看 342.6K关注 0票数 515

printNSLogprintln之间有什么区别?我应该在什么时候使用它们?

例如,在Python语言中,如果我想打印一个字典,我只需使用print myDict,但现在我有另外两个选项。我应该如何以及何时使用每一个?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2014-09-21 01:40:42

几个不同之处:

  1. print vs println

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

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

回到Swift 1.x中,print不会在打印字符串的末尾添加换行符,而println会这样做。但是现在,print总是在字符串的末尾添加换行符,如果您不希望这样做,可以提供一个terminator参数"".

  1. NSLog

代码语言:javascript
复制
- `NSLog` adds a timestamp and identifier to the output, whereas `print` will not;
代码语言:javascript
复制
- `NSLog` statements appear in both the device’s console and debugger’s console whereas `print` only appears in the debugger console.
代码语言:javascript
复制
- `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-0700MyApp28937:1751492 3.1416

代码语言:javascript
复制
- `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

  1. Effective iOS 14/MacOS11,我们有到“统一日志”系统的Logger接口。有关Logger的介绍,请参阅WWDC2020 Explore logging in Swift.

代码语言:javascript
复制
- To use `Logger`, you must import `os`:

导入操作系统

代码语言:javascript
复制
- Like `NSLog`, unified logging will output messages to both the Xcode debugging console and the device console, too
代码语言:javascript
复制
- Create a `Logger` and `log` a message to it:

logger =Logger(子系统: Bundle.main.bundleIdentifier!,类别:“网络”) logger.log("url = (url)")

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

代码语言:javascript
复制
- You can specify different types of logging messages, either `.info`, `.debug`, `.error`, `.fault`, `.critical`, `.notice`, `.trace`, etc.:

logger.error("web服务未响应(error.localizedDescription)")

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

代码语言:javascript
复制
- 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 =

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

os_log("url = (url,隐私:.public)")

  1. 在iOS 14/MacOS11之前,iOS 10/MacOS10.12引入了用于“统一日志记录”的os_log。有关统一日志记录的一般介绍,请参阅WWDC 2016视频Unified Logging and Activity Tracing.

代码语言:javascript
复制
- Import `os.log`:

导入os.log

代码语言:javascript
复制
- You should define the `subsystem` and `category`:

let log =OSLog(子系统: Bundle.main.bundleIdentifier!,类别:“网络”)

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

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

代码语言:javascript
复制
- You can specify different types of logging messages, either `.info`, `.debug`, `.error`, `.fault` (or `.default`):

os_log("web服务未响应“,类型:.error)

代码语言:javascript
复制
- You cannot use string interpolation when using `os_log`. For example with `print` and `Logger` you do:

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

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

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

公共os_log("url =%{

代码语言:javascript
复制
- 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:

}@“,url.absoluteString)

代码语言:javascript
复制
- You can also use the “Points of Interest” log if you want to watch ranges of activities from Instruments:

让pointsOfInterest =OSLog(子系统: Bundle.main.bundleIdentifier!,类别:.pointsOfInterest)

并使用以下命令开始范围:

os_signpost(.begin,日志: pointsOfInterest,名称:“网络请求”)

并以以下内容结束:

os_signpost(.end,日志: pointsOfInterest,名称:“网络请求”)

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

归根结底,对于使用Xcode的简单日志记录,print就足够了,但是统一日志记录(无论是Logger还是os_log)实现了同样的功能,但提供了更大的功能。

在调试必须在Xcode外部测试的iOS应用程序时,统一日志记录的威力变得非常明显。例如,在测试后台iOS应用程序进程时,连接到Xcode调试器changes the app lifecycle。因此,您经常希望在物理设备上进行测试,从设备本身运行应用程序,而不是从Xcode的调试器启动应用程序。通过统一日志记录,您仍然可以从macOS控制台应用程序查看iOS设备日志语句。

票数 862
EN

Stack Overflow用户

发布于 2015-06-17 02:32:45

如果您使用的是Swift 2,那么现在只能使用print()将某些内容写入输出。

苹果已经将println()print()函数合并为一个函数。

已更新到iOS 9

默认情况下,该函数通过添加换行符来终止打印的行。

代码语言:javascript
复制
print("Hello Swift")

终结器

要打印后无换行符的值,请传递一个空字符串作为终止符

代码语言:javascript
复制
print("Hello Swift", terminator: "")

分隔符

现在,您可以使用分隔符连接多个项目

代码语言:javascript
复制
print("Hello", "Swift", 2, separator:" ")

对两个执行

或者,您可以通过以下方式组合使用

代码语言:javascript
复制
print("Hello", "Swift", 2, separator:" ", terminator:".")
票数 84
EN

Stack Overflow用户

发布于 2015-12-04 10:57:28

此外,Swift 2有debugPrint() (和CustomDebugStringConvertible协议)!

不要忘记debugPrint(),它的工作原理与print()类似,但与most suitable for 类似。

示例:

  • Strings
    • print("Hello World!")成为
    • debugPrint("Hello World!") (Quotes!)

成为Hello World "Hello World"

  • Ranges
    • print(1..<6)变成了Range(1..<6)

  • debugPrint(1..<6)变成了1..<6

任何类都可以通过CustomDebugStringConvertible协议自定义其调试字符串表示。

票数 64
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25951195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档