快速打印而无需换行


109

在Swift 2.0中,会print()自动添加换行符。在迅速1.2,println()print()以前是独立的功能。因此,我如何打印一些文本而不在其中添加换行符,因为swift不再具有不添加换行符的打印功能。



3
@DánielNagy这不是重复的,因为swift 2.0没有println函数。我不问如何打印到控制台,因为打印功能可以做到这一点。我问“如何在不将换行符附加到输入文本的情况下打印到控制台”
Ankit Goel

2
Xcode 7 beta发行说明中明确记录了这一点:“ println和print已合并到一个带有默认参数的单个print函数中……”
Martin R

Answers:


207

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

print("Hello", terminator:"")

即使您仅看到带有seperator参数的界面。您可以简单地忽略它,因为它具有默认值func print(items: Any..., separator: String = default, terminator: String = default)
Binarian

2
有人会在Swift文档中找到什么地方default等于?
7stud,2016年

1
@ 7stud default是默认值的占位符。理想情况下,文档应包含实际值,而不是占位符。
苏珊(Sulthan)

1
默认值为:分隔符“”(单个空格)和终止符\ n(换行)
selva

66

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

用于打印的方法签名看起来像这样,

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("First of all", to: &myStream )
print("Then after", to: &myStream)
print("And, finally", to: &myStream)

打印到文件:

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

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

> po person

结果将是这样,

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

在Xcode 7beta4中对我也不起作用。调用进行print("foo", appendNewLine: false)编译,但是输出是,(foo), false并且无论如何都会添加新行!
mluisbrown,2015年

@mluisbrown appendNewline(小写l
JeremyP,2015年

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"

默认情况下,分隔符为空格" "
Jayram Kumar

4

如果要在循环中显示同一行:

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

输出:1 2 3 4

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.