输入输出基础

主要学习使用swift怎么输入输出

输出

print()函数语法

print(items: separator: terminator:)
`

使用实例

例子1:基本用法

没有使用terminator 默认为”\n”

print("Hello AisLing!")
print("Hello Swift")
`

Output

Hello AisLing!
Hello Swift
`

例子2:打印指定结尾符号

print("Hello AisLing!", terminator: " ") 

print("Hello Swift")
`

Output

Hello AisLing! Hello Swift
`

例子3:打印带分隔符

使用”. “分割多个dayitems

print("New Year", 2022, "See you soon!", separator: ". ")
`

Output

New Year. 2022. See you soon!
`

例子4:打印变量与字符串

let count = 10
let fruteType = "apple"
print ("Hello \(count) \(fruteType)")
print ("Hello" + " \(count) " +  fruteType)
`

Output

Hello 10 apple
Hello 10 apple
`

输入

输入的场景不适用Xcode playground场景

readLine()函数

/// - Parameter strippingNewline: If `true`, newline characters and character
///   combinations are stripped from the result; otherwise, newline characters
///   or character combinations are preserved. The default is `true`.
/// - Returns: The string of characters read from standard input. If EOF has
///   already been reached when `readLine()` is called, the result is `nil`.
public func readLine(strippingNewline: Bool = true) -> String?
`

例子1:基本用法

print("Enter your favorite programming language:")
if let name = readLine() {
   print("Your favorite programming language is \(name).")
}
`

Output

Enter your favorite programming language:
Swift
Your favorite programming language is Swift.
`