在swift

时间:2015-06-16 10:39:03

标签: swift swift3 swift2

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

4 个答案:

答案 0 :(得分:167)

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

print("Hello", terminator:"")

答案 1 :(得分:62)

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

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

答案 2 :(得分:9)

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

答案 3 :(得分:4)

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

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

输出:1 2 3 4