There's another method called dump()
which can also be used for logging:
func dump<T>(T, name: String?, indent: Int, maxDepth: Int, maxItems: Int)
Dumps an object’s contents using its mirror to standard output.
If you're using Swift 2, now you can only use print() to write something to the output.
Apple has combined both println() and print() functions into one.
Updated to iOS 9
By default, the function terminates the line it prints by adding a line break.
print("Hello Swift")
Terminator
To print a value without a line break after it, pass an empty string as the terminator
print("Hello Swift", terminator: "")
Separator
You now can use separator to concatenate multiple items
print("Hello", "Swift", 2, separator:" ")
Both
Or you could combine using in this way
print("Hello", "Swift", 2, separator:" ", terminator:".")
To add to Rob's answer, since iOS 10.0, Apple has introduced an entirely new "Unified Logging" system that supersedes existing logging systems (including ASL and Syslog, NSLog), and also surpasses existing logging approaches in performance, thanks to its new techniques including log data compression and deferred data collection.
From Apple:
The unified logging system provides a single, efficient, performant API for capturing messaging across all levels of the system. This unified system centralizes the storage of log data in memory and in a data store on disk.
Apple highly recommends using os_log
going forward to log all kinds of messages, including info, debug, error messages because of its much improved performance compared to previous logging systems, and its centralized data collection allowing convenient log and activity inspection for developers. In fact, the new system is likely so low-footprint that it won't cause the "observer effect" where your bug disappears if you insert a logging command, interfering the timing of the bug to happen.
You can learn more about this in details here.
To sum it up: use print()
for your personal debugging for convenience (but the message won't be logged when deployed on user devices). Then, use Unified Logging (os_log
) as much as possible for everything else.
NSLog
- add meta info (like timestamp and identifier) and allows you to output 1023 symbols. Also print message into Console. The slowest method@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 stringos_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)")
debugPrint()
(and CustomDebugStringConvertible
protocol)!Don't forget about debugPrint()
which works like print()
but most suitable for debugging.
Examples:
print("Hello World!")
becomes Hello World
debugPrint("Hello World!")
becomes "Hello World"
(Quotes!)print(1..<6)
becomes 1..<6
debugPrint(1..<6)
becomes Range(1..<6)
Any class can customize their debug string representation via CustomDebugStringConvertible
protocol.
Source: Stackoverflow.com