Swift Print ExampleUse the print method to display strings and other values to the console.
Print. Some programs need an artistic user interface. Some do not. In Swift, we can develop console programs that output data to a terminal.
For debugging, many developers prefer print statements. These help us track control flow in programs. No complicated interfaces are required.
Example statements. This Swift program uses many calls to print strings and other values to the console. In Xcode these will appear in an output section of the window.
Tip We can pass many types of objects to print(), such as Strings and Ints. For clear code, it is best not to convert to a string.
Print In Swift 3, this adds a newline after the argument. We can specify a separator and terminator.
Interpolation We can use the string interpolation syntax to write more complex data. We use an escaped parenthesis syntax.
Swift program that uses print
// Call print with strings. // ... No newline is inserted. print("one ") print("two ") // An Int can be printed. print(3) // Print can handle multiple arguments. // ... Specify a separator and a terminator. print("cat", "dog", "bird", separator: ";", terminator: ".\n") // Print a bool. print(true)
one two 3 cat;dog;bird. true
No newline. Sometimes we want to call print() and have no trailing newline. We must specify the "terminator" as an empty string. This avoids a trailing newline.
Here We loop over the characters in an array. We then print them all on the same line, with no separating newlines.
Swift program that uses print, empty terminator
var letters: [Character] = ["a", "b", "c", "d"] // Loop over characters in array. for c in letters { // Print with no terminator. print(c, terminator: "") }
String interpolation. Often we need to print multiple variables in a single line, with some text in between them (like labels). String interpolation is ideal here.
Tip We use escaped parentheses to begin variable lookups in a string interpolation. Here we separate two values with a ":" character.
Swift program that uses print, string interpolation
var number = 10 var title = "The Sound and the Fury" // Print with format string. // ... String interpolation inserts both variables. print("\(number): \(title)")
10: The Sound and the Fury
CustomStringConvertible. The print function will access a description property on types that implement CustomStringConvertible. This way we can improve print output of classes.
Here The Box class inherits from CustomStringConvertible and we provide a description. Print uses the description to write to the screen.
Caution The description for Box is not a good example—it should include fields. But it shows how the property works.
Swift program that uses print, CustomStringConvertible
class Box: CustomStringConvertible { var description: String { // Return a string that represents this instance. return "Box string representation" } } // Create new instance of class. let b = Box() // Print with CustomStringConvertible. print(b)
Box string representation
A review. With print, a versatile method, we write to the console. Swift works well for console programs. But apps are a primary target as well.
© 2007-2021 sam allen. see site info on the changelog