print without newline in swift

Starting in Swift 2.0, the recommended method of printing without newline is:

print("Hello", terminator: "")

print function has changed completely since the late revision of Swift, now it looks much simpler and there are variant of method to print to standard console.

The method signature for print looks something like this,

public func print<Target>(_ items: Any..., separator: String = default, terminator: String = default, to output: inout Target) where Target : TextOutputStream

And here are some usecases,

print("Swift is awesome.")
print("Swift", "is", "awesome", separator:" ")
print("Swift", "is", "awesome", separator:" ", terminator:".")

Prints:

Swift is awesome.
Swift is awesome
Swift is awesome.

Concatenating

print("This is wild", terminator: " ")
print("world")

Prints:

This is wild world

So, using terminator, you should be careful that the contents are relevant to same line.

Printing Object with CustomStringConvertible

struct Address {
  let city: String
}

class Person {
  let name = "Jack"
  let addresses = [
    Address(city: "Helsinki"),
    Address(city: "Tampere")
  ]
}

extension Person: CustomStringConvertible {
  var description: String {
    let objectAddress = unsafeBitCast(self, to: Int.self)
    return String(format: "<name: \(name) %p>", objectAddress)
  }
}

let jack = Person()
print(jack)

Prints:

<name: Jack 0x608000041c20>

CustomDebugStringConvertible

extension Person: CustomDebugStringConvertible {
  var debugDescription: String {
    let objectAddress = unsafeBitCast(self, to: Int.self)

    let addressString = addresses.map { $0.city }.joined(separator: ",")
    return String(format: "<name: \(name), addresses: \(addressString) %p>",objectAddress)
  }
}

Now, with lldb, you can use po command and it will print object as this in lldb console,

<name: Jack, addresses: Helsinki,Tampere 0x60c000044860>

Logging to file using TextOutputStream

struct MyStreamer: TextOutputStream {

  lazy var fileHandle: FileHandle? = {
    let fileHandle = FileHandle(forWritingAtPath: self.logPath)
    return fileHandle
  }()

  var logPath: String = "My file path"

  mutating func write(_ string: String) {
    fileHandle?.seekToEndOfFile()
    fileHandle?.write(string.data(using:.utf8)!)
  }
}

Now, using print to stream,

print("First of all", to: &myStream )
print("Then after", to: &myStream)
print("And, finally", to: &myStream)

Prints to file:

First of all
Then after
And finally

CustomReflectable

extension Person: CustomReflectable {
  var customMirror: Mirror {
    return Mirror(self, children: ["name": name, "address1": addresses[0], "address2": addresses[1]])
  }
}

Now, in lldb debugger, if you use command po,

> po person

Result would be something like this,

▿ <name: Jack, addresses: Tampere Helsinki  0x7feb82f26e80>
  - name : "Jack"
  ▿ address1 : Address
    - city : "Helsinki"
  ▿ address2 : Address
    - city : "Tampere"