Swift – Read User Input from Console

To read user input from console in Swift, use readLine() function. readLine() function returns the string of characters read from standard input, which in this case is console.

Examples

In the following example, we read the name of the user from console and greet the user with a message in the console.

main.swift

</>
Copy
print("Enter you name : ", terminator: "")
if let input = readLine() {
    print("Hello \(input)!")
}

Output

Conclusion

In this Swift Tutorial, we learned how to read user input from console using readLine() function, with the help of examples.