打印,NSLog和println之间的区别是什么,什么时候我应该使用它们?

例如,在Python中,如果我想打印一个字典,我只打印myDict,但现在我有两个其他选择。我应该如何以及何时使用它们?


当前回答

苹果日志

NSLog - add meta info (like timestamp and identifier) and allows you to output 1023 symbols. Also print message into Console. The slowest method. Is not safe because other applications has an access to log file @import Foundation NSLog("SomeString") print - prints all string to Xcode. Has better performance than previous @import Foundation print("SomeString") println (only available Swift v1) and add \n at the end of string os_log (from iOS v10) - prints 32768 symbols also prints to console. Has better performance than previous @import os.log os_log("SomeIntro: %@", log: .default, type: .info, "someString") Logger (from iOS v14) - prints 32768 symbols also prints to console. Has better performance than previous @import os let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "someCategory") logger.log("\(s)")

其他回答

一些不同之处:

print vs println: The print function prints messages in the Xcode console when debugging apps. 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. 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 adds a timestamp and identifier to the output, whereas print will not; NSLog statements appear in both the device’s console and debugger’s console whereas print only appears in the debugger console. NSLog in iOS 10-13/macOS 10.12-10.x uses printf-style format strings, e.g. NSLog("%0.4f", CGFloat.pi) that will produce: 2017-06-09 11:57:55.642328-0700 MyApp[28937:1751492] 3.1416 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.) Nowadays, while NSLog still works, we would generally use “unified logging” (see below) rather than NSLog. 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. To use Logger, you must import os: import os Like NSLog, unified logging will output messages to both the Xcode debugging console and the device console, too Create a Logger and log a message to it: let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "network") logger.log("url = \(url)") 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. 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)") 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. 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 = <private> 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: logger.log("url = \(url, privacy: .public)") 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. Import os.log: import os.log You should define the subsystem and category: let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "network") When using os_log, you would use a printf-style pattern rather than string interpolation: os_log("url = %@", log: log, url.absoluteString) You can specify different types of logging messages, either .info, .debug, .error, .fault (or .default): os_log("web service did not respond", type: .error) You cannot use string interpolation when using os_log. For example with print and Logger you do: logger.log("url = \(url)") But with os_log, you would have to do: os_log("url = %@", url.absoluteString) 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) 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) And start a range with: os_signpost(.begin, log: pointsOfInterest, name: "Network request") And end it with: os_signpost(.end, log: pointsOfInterest, name: "Network request") For more information, see https://stackoverflow.com/a/39416673/1271826.

最重要的是,print对于Xcode的简单日志记录来说已经足够了,但是统一日志记录(无论是Logger还是os_log)实现了同样的事情,但提供了更强大的功能。

当调试iOS应用程序时,必须在Xcode之外进行测试,统一日志记录的力量就会显现出来。例如,当测试后台iOS应用程序进程(如后台获取)时,连接到Xcode调试器会改变应用程序的生命周期。所以,你经常会想在物理设备上测试,从设备本身运行应用,而不是从Xcode的调试器启动应用。统一的日志记录让您仍然可以从macOS控制台应用程序查看您的iOS设备日志语句。

如果你正在使用Swift 2,现在你只能使用print()向输出写入一些东西。

Apple已经将println()和print()函数组合成 一个。

更新至iOS 9

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

print("Hello Swift")

《终结者》

若要打印后面没有换行符的值,请传递一个空字符串作为结束符

print("Hello Swift", terminator: "")

分隔符

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

print("Hello", "Swift", 2, separator:" ")

Both

或者你可以这样结合使用

print("Hello", "Swift", 2, separator:" ", terminator:".")

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

不要忘记debugPrint(),它的工作方式类似于print(),但最适合调试。

例子:

字符串 print("Hello World!")变成Hello World debugPrint("Hello World!")变成"Hello World"(引用!) 范围 Print(1..<6)变成1..<6 debugPrint(1..<6)变成Range(1..<6)

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

还有另一个方法叫做dump(),也可以用于日志记录:

<T>(T, name:字符串?,缩进:Int, maxDepth: Int, maxItems: Int) 使用对象的镜像将对象的内容转储到标准输出。

来自Swift标准库函数

为了补充Rob的答案,自iOS 10.0以来,苹果推出了一个全新的“统一日志”系统,取代了现有的日志系统(包括ASL和Syslog, NSLog),并在性能上超越了现有的日志方法,这要归功于它的新技术,包括日志数据压缩和延迟数据收集。

从苹果公司:

统一的日志记录系统提供了一个单一的、高效的、高性能的API,用于跨系统的所有级别捕获消息。这个统一的系统将日志数据集中存储在内存和磁盘上的数据存储中。

Apple强烈建议使用os_log来记录所有类型的消息,包括信息、调试和错误消息,因为与以前的日志系统相比,它的性能有了很大的提高,而且它的集中式数据收集可以方便地为开发人员进行日志和活动检查。事实上,新系统的占用空间很可能很低,如果您插入一个日志记录命令,就不会导致错误消失的“观察者效应”,从而干扰错误发生的时间。

你可以在这里了解更多细节。

总结一下:为方便起见,使用print()进行个人调试(但在用户设备上部署时,消息不会被记录)。然后,对于其他所有内容,尽可能使用统一日志记录(os_log)。