Table of contents
1.
Introduction
2.
Swift Output
2.1.
Example 1: Using the print() function to print to the screen
2.1.1.
Output:
2.2.
Example 2: Printing constants, variables and literals
2.2.1.
Output:
2.3.
Example 3: Printing without a link break using the terminator parameter
2.3.1.
Output:
2.4.
Example 4: Printing multiple items using a single print function
2.4.1.
Output:
2.5.
Example 5: Printing multiple lines
2.5.1.
Output:
2.6.
Example 6: Printing variables using string interpolation
2.6.1.
Output:
3.
Swift Basic Input
3.1.
Example 1: Taking information from the user using readLine()
3.1.1.
Output:
4.
Frequently Asked Question
4.1.
How do you take input in Swift?
4.2.
How do I print a string in Swift?
4.3.
What is a stride in Swift?
5.
Conclusion
Last Updated: Mar 27, 2024

Swift Input and Output

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Input and output are terms used to describe the interaction between a computer program and its user. The user provides input to the program, whereas the programme provides output to the user. 

Let's understand with the help of an example. Let's say we're stuck in the lawyer's office waiting room and get hungry. Oh, there's a vending machine! We've been rescued! We may not be as enthusiastic, but let's get something to eat. We'll put $2 into the vending machine and get a bag of chips. This is a very straightforward and simple form of input and output. Put something into a machine, and it produces something else.

In a similar manner, in this blog, we will go through how to read the user's standard input in swift programming language and display the output to the screen in various ways. 

Swift Output

To begin, open XCode and create a new command-line application project.

To send output to standard output, simply use:

print( :separator:terminator:) function (screen)

Three parameters are recognised by the function print( :separator:terminator:).

• items: Things to print over a console. It can recognise many items.

• separator: a line that will be printed between each item. A single space (" ") is the default.

• terminator: After all items have been printed, print this string. A newline ("n") is the default.

Because the last two parameters (separator and terminator) have default values, they aren't required to be used when invoking the print function.

Example 1: Using the print() function to print to the screen

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

Output:

Hello, World!
I love Swift.

Print ("Hello, World!") in the above program prints the string literal Hello, World! To the terminal.

 

Note: Because the print strategy's parameter terminator has a default value of n, it also alters the line (adds a line break) when printing "I love Swift" (newline).

The sentence print("I love Swift.") is similar in that it publishes the message in a new line.

Example 2: Printing constants, variables and literals

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

Output:

Hello, World!
123.45

You can print the value of a variable or constant by simply typing the name of the variable or constant in the print function. print(helloMsg) returns the value Hello, World! From the variable helloMsg in the preceding program.

In the print statement, you can also use it literally. The print(123.45) sentence produces a floating-point literal 123.45 without the twofold quotation.

Example 3: Printing without a link break using the terminator parameter

If you need to print without a line break, use the terminator parameter of the print function to pass an empty string, such as

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

Output:

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

The terminator in the above program is the string printed after all of the elements have been printed. The terminator has been set to a blank string (default is a newline \n). The first sentence prints without a new line and the second statement print("I love Swift.") displays the message in a similar line.

 

The sentence print("I also love Computer Programming") outputs in a new line since the print("I adore Swift.") function adds a line break.

Example 4: Printing multiple items using a single print function

You may also print multiple elements in a single print statement and use a separator like this:

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

Output:

Hello, World!. 2021. See you soon.

In the print statement of the above program, we separated different data types using a comma. We've included the separator parameter with the value ".". The separator (dot.) is inserted between each item. As you can see, the output is separated by a space and a character.

Example 5: Printing multiple lines

If you need to print numerous lines with a single print statement, you can utilise the carriage return escape sequence in the print statement as follows:

print("Hello, \rWorld!")

Output:

Hello,
World!

Example 6: Printing variables using string interpolation

You can also use string interpolation to add the value of a variable or be consistent with the string literal, such as wrapping a variable in a set of parentheses and prefixing it with a backslash (\).

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

Output:

I have a message Hello, World!

The statement print("I have a message (helloMsg)") inserts the value of variable helloMsg into a string literal wrapped as (helloMsg). As a result, the statement prints out Hello, World! On the screen.

Swift Basic Input

If you want to take user input in Swift, you'll need to use the UIKit framework.

Example 1: Taking information from the user using readLine()

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

Output:

Please Enter your favourite programming language.
Swift
Your favourite programming language is Swift.
  • The print function in the above programme produces, “Enter your preferred programming language here”. In the debugging section, In the debug area, the statement let name = readLine() waits for user input.
  • The readLine() function reads user input and assigns it to the name variable.
  • The readLine() method does not return a standard string. Instead, an optional string is returned. As a result, the name! was used to unwrap the name aggressively.
  • If you input "Swift" and hit enter, the readLine function assigns the string to a consistent name and displays the result as Your favourite programming language is Swift.

We have strongly unwrapped the constant as the name! in the statement print("Your favourite programming language is (name!)") because the readLine work produces an optional string.

Frequently Asked Question

How do you take input in Swift?

We can't take input from the Xcode playground directly in Swift. However, we can use the readLine() function to collect feedback from users by creating a Command Line Tool in Xcode. Consider printing ("Enter your favourite programming language:") let readLine = name () print("(name!) is your favourite programming language.")

How do I print a string in Swift?

It's also possible to print full strings. A string is printed by giving it to the print() function, much as variables and constants. See what happens if you save this to your playground file: "I'm printing a string in Swift!" print("I'm printing a string in Swift!")

What is a stride in Swift?

Swift offers a handy stride() function that allows you to go from one integer to another with any increment and even specify whether the upper bound is exclusive or inclusive.

Conclusion

In this blog, we have extensively discussed the Swift Inputs and Outputs. We hope that this article has provided you with additional information about the Input and output statements in swift. And to learn in-depth about Swift functions, check out the course on our Swift on the Coding Ninjas website. And also, check out the awesome content on the Coding Ninjas Website, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview Experiences, Coding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog in helping other ninjas grow.

Delighted Programming!

Live masterclass