没有换行的快速打印

在 Swift 2.0中,print()会自动添加换行符。在 Swift 1.2中,println()print()曾经是独立的功能。因此,我如何打印一些文本,而不添加换行符,因为迅捷不再有一个打印功能,不附加换行符。

85061 次浏览

print的功能已经完全改变,因为后期修订的 斯威夫特,现在它看起来更简单,并有方法打印到标准控制台的变体。

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

这里有一些用例,

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

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

定制字符串可转换打印对象

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

现在,使用 Ldb,您可以使用 阿宝命令,它将在 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"

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

print("Hello", terminator: "")

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

如果你想在循环中使用相同的代码行:

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

产出: 1234

let description = String(describing: yourVariable)

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

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