Fortify your code with runtime checks, and examine your values' runtime representation.
Debugging and Reflection
Topics
Printing and Dumping
func print(Any, separator: String, terminator: String)Writes the textual representations of the given items into the standard output.
func print<Target>(Any, separator: String, terminator: String, to: inout Target)Writes the textual representations of the given items into the given output stream.
func debug Print(Any, separator: String, terminator: String)Writes the textual representations of the given items most suitable for debugging into the standard output.
func debug Print<Target>(Any, separator: String, terminator: String, to: inout Target)Writes the textual representations of the given items most suitable for debugging into the given output stream.
func dump<T>(T, name: String?, indent: Int, max Depth: Int, max Items: Int) -> TDumps the given object’s contents using its mirror to standard output.
func dump<T, Target Stream>(T, to: inout Target Stream, name: String?, indent: Int, max Depth: Int, max Items: Int) -> TDumps the given object’s contents using its mirror to the specified output stream.
Testing
func assert(() -> Bool, () -> String, file: Static String, line: UInt)Performs a traditional C-style assert with an optional message.
func assertion Failure(() -> String, file: Static String, line: UInt)Indicates that an internal sanity check failed.
func precondition(() -> Bool, () -> String, file: Static String, line: UInt)Checks a necessary condition for making forward progress.
func precondition Failure(() -> String, file: Static String, line: UInt) -> NeverIndicates that a precondition was violated.
Exiting a Program
func fatal Error(() -> String, file: Static String, line: UInt) -> NeverUnconditionally prints a given message and stops execution.
enum NeverThe return type of functions that do not return normally, that is, a type with no values.
Querying Runtime Values
struct MirrorA representation of the substructure and display style of an instance of any type.
struct Object IdentifierA unique identifier for a class instance or metatype.
func type<T, Metatype>(of: T) -> MetatypeReturns the dynamic type of a value.
func == (Any .Type?, Any .Type?) -> BoolReturns a Boolean value indicating whether two types are identical.
func != (Any .Type?, Any .Type?) -> BoolReturns a Boolean value indicating whether two types are not identical.
Customizing Your Type's Reflection
Provide a custom reflection for your types using these protocols.
protocol Custom ReflectableA type that explicitly supplies its own mirror.
protocol Custom Leaf ReflectableA type that explicitly supplies its own mirror, but whose descendant classes are not represented in the mirror unless they also override custom.
protocol Custom Playground Display ConvertibleA type that supplies a custom description for playground logging.
typealias Playground Quick LookThe sum of types that can be used as a Quick Look representation.
See Also
Programming Tasks
Print values to the console, read from and write to text streams, and use command line arguments.
Use key-path expressions to access properties dynamically.
Allocate and manage memory manually.
Perform casts between types or represent values of any type.
Use imported C types or call C variadic functions.
Work with prefix, postfix, and infix operators.