A type that can be the target of text-streaming operations.
SDK
- Xcode 8.0+
Framework
- Swift Standard Library
Declaration
Overview
You can send the output of the standard library’s print(_:
and dump(_:
functions to an instance of a type that conforms to the Text
protocol instead of to standard output. Swift’s String
type conforms to Text
already, so you can capture the output from print(_:
and dump(_:
in a string instead of logging it to standard output.
Conforming to the TextOutputStream Protocol
To make your custom type conform to the Text
protocol, implement the required write(_:)
method. Functions that use a Text
target may call write(_:)
multiple times per writing operation.
As an example, here’s an implementation of an output stream that converts any input to its plain ASCII representation before sending it to standard output.
The ASCIILogger
type’s write(_:)
method processes its string input by escaping each Unicode scalar, with the exception of "\n"
line returns. By sending the output of the print(_:
function to an instance of ASCIILogger
, you invoke its write(_:)
method.