Looking for a Swift equivalent of Cocoa's description
, I found the following protocols in Swift: Printable
and DebugPrintable
.
What's the difference between these two protocols and when should I use each one?
Debug. Print is one of the useful tools presented in the VBA editor. These scripts are primarily responsible for the creation and execution of macros in Microsoft software. read more to figure out how a program works. It helps to analyze the changes in the values of variables created in the VBA program.
Basic output in Swift using print The snippet above will display the Hello World! text followed by a newline character ( \n ), this is because the default terminator is always a newline. You can override this behavior by providing your own terminator string. print("Hello World!", terminator: "") // output: Hello World!
Dumps the given object's contents using its mirror to standard output and specified output stream. We can use also Dump for the same purpose as we used to print() but print() only prints the class name while in the case of dump() prints the whole class hierarchy at Xcode debug console.
Here is an example class
class Foo: Printable, DebugPrintable {
var description: String {
return "Foo"
}
var debugDescription: String {
return "debug Foo"
}
}
This is how to use it.
println(Foo())
debugPrintln(Foo())
Here is the output with no surprises:
Foo
debug Foo
I didn't try this in a Playground. It works in an actual project.
The answer above was for Swift 1. It was correct at the time.
Update for Swift 2.
println and debugPrintln are gone and the protocols have been renamed.
class Foo: CustomStringConvertible, CustomDebugStringConvertible {
var description: String {
return "Foo"
}
var debugDescription: String {
return "debug Foo"
}
}
print(Foo())
debugPrint(Foo())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With