print without newline in swift

91.5k views Asked by At

In swift 2.0, print() automatically adds a newline character. In swift 1.2, println() and print() used to be separate functions. So how do I print some text and not add a newline to it since swift no longer has a print function that does not append newlines.

5

There are 5 answers

4
cheniel On BEST ANSWER

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

print("Hello", terminator: "")
2
Sandeep On

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"
0
mykolaj On
let description = String(describing: yourVariable)

The description variable here would hold exactly the same output you would get in console by using print() function.
So you can replace anything in the output.
For example I use this code to print JSON in single line:

print(description.replacingOccurrences(of: "\n", with: " "))
1
Morten Gustafsson On

In Swift 2.0 you can do this:

Basic version

print("Hello World")
result "Hello World\n"

Using terminator

print("Hello World", terminator:"")
result "Hello World"

Using separator

print("Hello", "World", separator:" ")
result "Hello World\n"

Using separator and terminator

print("Hello", "World", separator:" ", terminator:"")
result "Hello World"

Using one variable

var helloworld = "Hello World"
print(helloworld)
result "Hello World\n"

Using two variables

var hello = "Hello"
var world = "World"
print (hello, world)
result "Hello World\n"
0
PVCS On

If you want same line in loop:

for i in 1...4
{
    print("", i, separator: " ", terminator:"")
}
print()

Output: 1 2 3 4