斯威夫特:print()vs println()vs NSLog() [英] Swift: print() vs println() vs NSLog()
问题描述
print
,NSLog
和println
有什么区别?什么时候应该使用它们?
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?
推荐答案
一些区别:
-
print
与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
总是在字符串的末尾添加换行符,如果您不希望这样做,请提供""
的参数""
.
Back in Swift 1.x, print
didn't 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
较慢;
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
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 10/macOS 10.12,还有第三种选择os_log
,它是统一日志记录"系统的一部分(请参阅WWDC 2016视频
Effective iOS 10/macOS 10.12, there is a third alternative, os_log
, part of the "unified logging" system (see WWDC 2016 video Unified Logging and Activity Tracing).
-
使用
os_log
函数之前,您必须导入os.log
:
You must import
os.log
before usingos_log
function:
import os.log
像NSLog
,os_log
一样,也会向Xcode调试控制台和设备控制台输出消息
Like NSLog
, os_log
will output messages to both the Xcode debugging console and the device console, too
您现在可以控制控制台"应用程序中可用的子系统"和类别"字段.例如:
You can now control the "subsystem" and "category" fields available in the Console app. For example:
let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "network")
os_log("url = %@", log: log, url.absoluteString)
通过外部控制台应用程序观察该应用程序时,不仅可以将这些列添加到主视图,还可以基于这些列进行过滤.想要将调试消息与(a)其他子系统代表您的应用程序生成的调试消息区分开来时,这非常有用.或(b)来自其他类别或类型的消息.
When you observe the app via the external Console app, you can not only add these columns to the main view, but you can filter on the basis of these. It's very useful when wanting 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
,.fault
(或.default
):
You can specify different types of logging messages, either .info
, .debug
, .error
, .fault
(or .default
):
os_log("web service did not respond", type: .error)
因此,如果使用外部控制台应用程序,则可以选择仅查看某些类别的消息(例如,如果在控制台的操作"菜单上选择包括调试消息",则仅显示调试消息).这些设置还规定了有关是否将内容记录到磁盘的许多细微问题的详细信息.有关更多详细信息,请参见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.
使用os_log
时不能使用字符串插值.例如,您不能执行以下操作:
You cannot use string interpolation when using os_log
. For example you cannot do:
os_log("foo \(url.absoluteString)")
您必须这样做:
os_log("url = %@", url.absoluteString)
出现上述限制的原因之一是为了支持数据隐私.默认情况下,原始数据类型(例如数字)是公共的,而对象(例如字符串)在默认情况下是私有的.在记录URL的上一个示例中,如果从设备本身调用了该应用程序,并且正在从Mac的Console应用程序观看,则将看到:
One of the reasons for the above limitation is to support data privacy. Primitive data types (e.g. numbers) are public by default and objects (e.g. strings) are private by default. In the previous example where you logged the URL, if the app were invoked from the device itself and you were watching from your Mac's Console app, you'd see:
url =< private>
url = <private>
如果要从外部设备查看它,则必须执行以下操作:
If you wanted to see it from external device, you'd have to do:
os_log("url = %{public}@", url.absoluteString)
注意,NSLog
现在在幕后使用统一的通知系统,但有以下警告:
Note, NSLog
now uses the unified notification system behind the scenes, but with the following caveats:
-
您无法控制子系统或类别或日志类型;
You cannot control the subsystem or category or log type;
它不支持隐私设置.
最底线,print
足以完成简单的任务,但是NSLog
很有用,因为它包含了您的时间戳信息.
Bottom line, print
is sufficient for simple tasks, but NSLog
is useful because it includes timestamp information for you.
在调试必须在Xcode之外进行测试的iOS应用程序时,os_log
的功能变得十分明显.例如,在测试后台iOS应用程序进程(例如后台获取)时,连接到Xcode调试器更改应用程序生命周期.因此,您经常需要在物理设备上进行测试,而不是通过Xcode的调试器从设备本身运行应用程序,而是从设备本身运行应用程序.统一日志记录让您仍然可以通过macOS控制台应用程序观看iOS设备os_log
语句.
The power of os_log
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 physical device, running app from device itself, not starting the app from Xcode’s debugger. Unified logging let’s you still watch your iOS device os_log
statements from the macOS Console app.
这篇关于斯威夫特:print()vs println()vs NSLog()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!