English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn different methods to display output and get input in Swift.
You can simply use the function print(items: Any..., separator: String = default, terminator: String = default) to send output to standard output (screen). Refer to the article for more information.Functions in Swift.
The function print(items, separator, terminator:) accepts three parameters.
items: The items to be printed in the console. It can accept multiple items.
separator: The string to be printed between each item. The default is a single space (" ").
terminator: The string to be printed after all items are printed. The default value is the newline character ("\n").
Since the last two parameters (separator, terminator) have been specified with default values, they do not need to be used forcibly when calling the print function.
print("Hello, World!") print("I love Swift.")
When running the program, the output is:
Hello, World! I love Swift.
In the above program, print("Hello, World!") outputs the string literalHello, World!。in the console.
You can see that in the print"I love Swift."It will also change the line (add a newline character).Because the terminator parameter of the print method has a default value \n (newline).
Therefore, the statement print("I love Swift.") outputs the message on a new line.
var helloMsg = "Hello, World!" print(helloMsg) print(123.45)
When running the program, the output is:
Hello, World! 123.45
You can print the value of a variable or constant by directly adding the name of the variable or constant to the print function. In the above program, print(helloMsg) outputs the value of the variable helloMsgHello, World!.
You can also insert literals in the print statement. In the statement print(123.45), it takes123.45Print a floating-point literal without quotes.
If you want to print output without a newline, you need toprintThe terminator parameter of the function is passed an empty string as shown below:
print("Hello, World!", terminator: "") print("I love Swift.") print("I also love Taylor Swift.")
When running the program, the output is:
Hello, World! I love Swift. I also love Taylor Swift.
In the above program, terminator is the string printed after all items are printed.
We passed an empty string as the terminator (the default is a newline character \n). Therefore, when printing the first statement, no new line is added, and the statement print("I love Swift.") displays the message on the same line.
Because the print("I love Swift.") function adds a newline character, the statement print("I also love Taylor Swift") is output on a new line.
You can also print multiple items in a print statement and add a separator between these items:
print("Hello, World!", 2020, "See you soon", separator: ". ")
When running the program, the output is:
Hello, World!. 2020. See you soon
In the above program, we added different data types in the print statement and separated them with commas.
The items to be printed are strings Hello, World!. 2020. See you soon.
We also passed the delimiter "." in the parameter. This will insert the delimiter between each item. So you can see the output results, separated by "." and followed by a space.
If you want to print multiple lines with a single print statement, you can use the escape sequence called carriage return \r in the print statement, as shown below:
print("Hello, \rWorld!")
When running the program, the output is:
Hello, World!
In Swift, there is a better way to output multiple lines of messages in a single print statement. You must use multiline string literals. This is done by adding characters as
print(""" Hello, World! ""
When running the program, the output is:
Hello, World!
You can also add the value of a variable or constant to a string literal by wrapping the variable in parentheses and prefixing it with a backslash (\).
var helloMsg = "Hello, World!" print("I have a message \(helloMsg)")
When running the program, the output is:
I have a message Hello, World!
The statement print("I have a message \(helloMsg)") inserts the value of the variable helloMsg by wrapping it in the string literal \(helloMsg). Therefore, the statement outputsFrom "I have a message Hello, World!" toon the screen.
If you want to get input from the user in Swift, you must enter it in the Xcode Playground without using the UIKit framework.
However, using the Swift framework, you can create a command-line application in Xcode to get input from the user.
This is the code you can use to get user input.
print("Please enter your favorite programming language", terminator: ".") let name = readLine() print("What is your favorite programming language is \(name!)"}
When running the program, the output is:
Please enter your favorite programming language. Swift What is your favorite programming language is Swift.
In the above program, the print function outputsPlease enter your favorite programming language.in the Debug Area. The statement let name = readLine() waits for the user to input in the Debug Area.
If you type "Swift" and press Enter, the readLine function will assign the string to the constant name and display it as Your favorite programming language is Swift.
Since the readLine function returns an optional string, we force unwrap the constant just like in the name! declaration in the statement print("Your favorite programming language is \(name!)").
Since the readLine function returns an optional string, we force unwrap the constant to name! in the statement print("Your favorite programming language is \(name!)"),
You will learn in the articleIn Swift OptionalsLearn more about optionals.