in , ,

Swift Basic Input and Output

Swift Basic Input and Output
Swift Basic Input and Output

Swift Basic Input and Output: In this tutorial, you will learn various approaches to display output and get input in Swift.

Swift Basic Output

You can just use the print(_:separator:terminator:) function to send output to standard output (screen).

The function print(_:separator:terminator:) acknowledges three parameters.

• items: Items to print in the console. It can acknowledge more than one item.

• separator: A string to print between each item. The default is a single space (” “).

• terminator: The string to print after all items have been printed. The default is a newline (“\n”).

Since, last two parameters (separator, terminator) has default values previously determined, it isn’t mandatory to use them while calling the print function.


Example 1: Printing to the screen using simple print() function

print("Hello, World!")
print("I love Swift.")

At the point when you run the program, the output will be:

Hello, World!
I love Swift.

In the above program, print(“Hello, World!”) outputs string literal Hello, World! in the console.

You can see it likewise changes the line (adds a line break) when printing “I love Swift.” since print strategy’s parameter terminator has a default value \n (new line).

Along these lines, the statement print(“I love Swift.”) outputs the message in new line.


Example 2: Printing constants, variables and literals

var helloMsg = "Hello, World!"
print(helloMsg)
print(123.45)

At the point when you run the program, the output will be:

Hello, World!
123.45

You can print the value of variable or constant by adding variable or consistent name straightforwardly in the print function. In the above program print(helloMsg) outputs the value Hello, World! of the variable helloMsg.

You can likewise insert literals in the print statement. In the statements, print(123.45), it takes a floating-point literal 123.45 without the twofold quote and prints it.


Example 3: Printing without a link break using terminator parameter

On the off chance that you need to print without a line break, you need to pass an unfilled string in the terminator parameter of the print function as:

print("Hello, World!", terminator: "")
print("I love Swift.")
print("I also love Computer Programming.")

At the point when you run the program, the output will be:

Hello, World!I love Swift.
I also love Computer Programming.

In the above program, terminator is the string that is printed after all items has been printed.

We have passed a vacant string as the terminator (default is a newline \n). In this way, the first statement prints without adding a new line, and the statement print(“I love Swift.”) displays the message in a similar line.

Since, print(“I love Swift.”) function adds a line break, the statement print(“I also love Computer Programming”) output in a new line.


Example 4: Printing multiple items using single print function

You can likewise print various items in a single print statement and add separator between those items as:

print("Hello, World!", 2021, "See you soon", separator: ". ")

At the point when you run the program, the output will be:

Hello, World!. 2021. See you soon

In the above program, we have added different data types in the print statement separated by a comma.

The items to print are string Hello, World!, int 2020 and string See you soon.

We have additionally passed the “.” value in the separator parameter. This inserts the separator (dot.) between each item. So you can see the output separated by. character followed by a space.


Example 5: Printing multiple lines

On the off chance that you need to print in multiple lines with a single print statement, you can use escape sequence \r known as carriage return in the print statement as:

print("Hello, \rWorld!")

At the point when you run the program, the output will be:

Hello, 
World!

Example 6: Printing multiple lines using triple quotes

In Swift, there is a better method to output multiline messages in a single print statement. You need to use a multiline string literal. This is finished by adding characters inside multiline string literal using triple quotes as

print("""
Hello,
World!
""")

At the point when you run the program, the output will be:

Hello,
World!

Example 7: Printing variables using string interpolation

You can likewise add the worth of a variable or consistent into the string literal by using string interpolation, for example wrapping a variable by a couple of parentheses, prefixed by a backslash (\).

var helloMsg = "Hello, World!"
print("I have a message \(helloMsg)")

At the point when you run the program, the output will be:

I have a message Hello, World!

The statement print(“I have a message (helloMsg)”) inserts the worth of variable helloMsg by wrapping it as (helloMsg) in string literal. Hence, the statement outputs I have a message Hello, World! on the screen.


Swift Basic Input

In the event that you need to take input from the user in Swift, you can’t do it in the Xcode playground without using the UIKit framework.

Nonetheless, using the Swift framework, you can create a Command Line Application in Xcode that takes input from a user. You can see Swift Command Line Application tutorial for creating a command-line application using Xcode.

Here’s the code you can use to get input from user.

Example 8: Taking input from the user using readLine()

print("Please Enter your favorite programming language", terminator: ".")
let name = readLine()
print("Your favorite programming language is \(name!).")

At the point when you run the program, the output will be:

Please Enter your favorite programming language.
Swift
Your favorite programming language is Swift.

In the above program, the print function outputs Please Enter your favorite programming language. in the debug area. The statement let name = readLine()waits for user input in the debug area.

In the event that you type “Swift” and press enter, the readLine work appoints that string to a consistent name and displays the output as Your favorite programming language is Swift.

Since the readLine work returns an optional string, we have strongly unwrapped the constant as the name! in the statement print(“Your favorite programming language is (name!)”).

You’ll learn more about optionals in the article: Swift Optionals.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

HTML Quotation and Citation Elements

HTML Quotation and Citation Elements

HTML Comments

HTML Comments