A type that can be the target of text-streaming operations.
You can send the output of the standard library's print(_:to:) and
dump(_:to:) functions to an instance of a type that conforms to the
TextOutputStream protocol instead of to standard output. Swift's
String type conforms to TextOutputStream already, so you can capture
the output from print(_:to:) and dump(_:to:) in a string instead of
logging it to standard output.
vars= ""
fornin1...5{
print(n, terminator: "", to: &s)
}
// s == "12345"
Conforming to the TextOutputStream Protocol
To make your custom type conform to the TextOutputStream protocol,
implement the required write(_:) method. Functions that use a
TextOutputStream 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(_:to:) function to an instance of
ASCIILogger, you invoke its write(_:) method.
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.