protocol TextOutputStream
Inheritance | View Protocol Hierarchy → |
---|---|
Import | import Swift |
Instance Methods
mutating func write(_:)
Required
Appends the given string to the stream.
Declaration
mutating func write(_ string: String)
A type that can be the target of text-streaming operations.
You can send the output of the standard library's
print(_:to:)
anddump(_:to:)
functions to an instance of a type that conforms to theTextOutputStream
protocol instead of to standard output. Swift'sString
type conforms toTextOutputStream
already, so you can capture the output fromprint(_:to:)
anddump(_:to:)
in a string instead of logging it to standard output.Conforming to the TextOutputStream Protocol
To make your custom type conform to the
TextOutputStream
protocol, implement the requiredwrite(_:)
method. Functions that use aTextOutputStream
target may callwrite(_:)
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'swrite(_:)
method processes its string input by escaping each Unicode scalar, with the exception of"\n"
line returns. By sending the output of theprint(_:to:)
function to an instance ofASCIILogger
, you invoke itswrite(_:)
method.