swift2 在swift中不使用换行符打印

yruzcnhs  于 2022-11-06  发布在  Swift
关注(0)|答案(5)|浏览(291)

在swift 2.0中,print()会自动添加一个换行符。在swift 1.2中,println()print()曾经是两个独立的函数。那么,既然swift不再有不添加换行符的打印函数,我该如何打印一些文本而不添加换行符呢?

vyu0f0g1

vyu0f0g11#

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

print("Hello", terminator: "")
vkc1a9a2

vkc1a9a22#

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>

自定义调试字符串可转换

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>

使用 * 文本输出流*记录到文件

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

可自定义反射

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"
8zzbczxx

8zzbczxx3#

在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"
wribegjk

wribegjk4#

如果要在循环中使用同一行:

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

输出:1 2 3 4

ufj5ltwl

ufj5ltwl5#

let description = String(describing: yourVariable)

这里的description变量将保存与您在控制台中使用print()函数获得的输出完全相同的输出。
因此可以替换输出中的任何内容。
例如,我使用以下代码在一行中打印JSON:

print(description.replacingOccurrences(of: "\n", with: " "))

相关问题