127

在 swift 2.0 中,print()自动添加换行符。在 swift 1.2 中,println()曾经print()是单独的函数。那么我如何打印一些文本而不添加换行符,因为 swift 不再具有不附加换行符的打印功能。

4

4 回答 4

241

从 Swift 2.0 开始,推荐的不带换行符的打印方法是:

print("Hello", terminator: "")
于 2015-08-28T07:56:56.777 回答
70

print自Swift的最新版本以来,功能已经完全改变,现在它看起来更简单,并且有多种方法可以打印到标准控制台。

print 的方法签名看起来像这样,

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

这里有一些用例,

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

印刷:

Swift is awesome.
Swift is awesome
Swift is awesome.

连接

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

印刷:

This is wild world

因此,使用终止符时,您应该注意内容与同一行相关。

使用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)

印刷:

<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)
  }
}

现在,使用lldb,您可以使用po 命令,它将在 lldb 控制台中打印对象,

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

使用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)!)
  }
}

现在,使用 print 流式传输,

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

打印到文件:

First of all
Then after
And finally

自定义反射

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

现在,在 lldb 调试器中,如果你使用命令 po,

> po person

结果会是这样的,

▿ <name: Jack, addresses: Tampere Helsinki  0x7feb82f26e80>
  - name : "Jack"
  ▿ address1 : Address
    - city : "Helsinki"
  ▿ address2 : Address
    - city : "Tampere"
于 2015-06-16T10:41:26.700 回答
11

在 Swift 2.0 中,您可以这样做:

基础版

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

使用终结器

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

使用分隔符

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

使用分隔符和终止符

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

使用一个变量

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

使用两个变量

var hello = "Hello"
var world = "World"
print (hello, world)
result "Hello World\n"
于 2016-02-29T09:06:11.307 回答
4

如果你想要循环中的同一行:

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

输出:1 2 3 4

于 2017-05-01T12:40:34.723 回答