Home
Swift
print Example
This page was last reviewed on Jan 11, 2024.
Dot Net Perls
Print. In Swift 5.8, we can develop console programs that output data to a terminal. The print() method automatically inserts a newline, but this can be changed.
For debugging, many developers prefer print statements—these help us track control flow in programs. No complicated interfaces are required.
TextOutputStream
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.
Info Print() adds a newline after the argument. We can specify a separator and terminator.
Also We can use the string interpolation syntax to write more complex data. We use an escaped parenthesis syntax.
// 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.
var letters: [Character] = ["a", "b", "c", "d"] // Loop over characters in array. for c in letters { // Print with no terminator. print(c, terminator: "") }
abcd
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.
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.
Warning The description for Box is not a good example—it should include fields. But it shows how the property works.
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
Summary. With print, a versatile method, we write to the console. Swift works well for console programs, but apps are a primary target as well.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 11, 2024 (edit link).
Home
Changes
© 2007-2024 Sam Allen.