Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Printable and DebugPrintable in Swift

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?

like image 224
cfischer Avatar asked Sep 05 '14 11:09

cfischer


People also ask

What is a debug print?

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.

How do I print a log in Swift?

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!

What is dump in Swift?

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.


1 Answers

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())
like image 158
Gene De Lisa Avatar answered Oct 03 '22 07:10

Gene De Lisa