Updated
Aug 21, 2021 8:38 AM
Created
Aug 21, 2021 8:15 AM
Tags
Swift
struct LogMessageInterpolation: StringInterpolationProtocol {
var value: String = ""
typealias StringLiteralType = String
@inlinable public init(
literalCapacity: Int,
interpolationCount: Int
) {
self.value.reserveCapacity(literalCapacity)
}
mutating func appendLiteral(_ literal: String) {
self.value.append(literal)
}
mutating func appendInterpolation<T>(_ value: T) where T: CustomStringConvertible {
self.value.append(String(describing: value))
}
mutating func appendInterpolation(count: Int) {
self.value.append("count: \(count)")
}
}
struct LogMessage: ExpressibleByStringLiteral, ExpressibleByStringInterpolation {
public typealias StringInterpolation = LogMessageInterpolation
public var body: String
public init(
stringLiteral value: String
) {
self.body = value
}
init(
stringInterpolation: LogMessageInterpolation
) {
self.body = stringInterpolation.value
}
}
let logMessage: LogMessage = "Hello \(128), \(count: 3)"
print(logMessage.body)
Hello 128, count: 3
DefaultStringInterpolation
Examples
logger.log("Ordered smoothie \(smoothieName, privacy: .public)")
public func log(_ message: OSLogMessage)
@frozen struct OSLogMessage
- It conforms to
ExpressibleByStringLiteral
- And
ExpressibleByStringInterpolation
OSLogInterpolation
- It conforms to
StringInterpolationProtocol