Yogesh Chauhan's Blog

How to read Standard Input in Swift?

in Swift on March 5, 2020

To read standard input in swift we will need to use readLine() function.

Function


readLine(strippingNewline:)

Returns a string read from standard input through the end of the current line or until EOF is reached.

Declaration


func readLine(strippingNewline: Bool = true) -> String?

Reading String


let string_input = readLine()
if let string_input = string_input {
    print(string_input) 
}

Reading Int


let number_input = Int(readLine()!)
if let number_input = number_input {
    print(number_input)
}

Reading Float


let number_input = Float(readLine()!)
if let number_input = number_input {
    print(number_input)
}

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
What is the difference between let and var in Swift?SwiftRecursive WITH Queries in Postgres (Common Table Expressions)PostgresArbitrary Arguments in SCSS functionsSCSSHow to center an image horizontally and vertically?CSSHow to scroll contents of HTML body horizontally and vertically using JavaScript?JavaScriptHow to get user’s Browser and Operating Systems information using PHP?PHPHow to create a cross-browser smooth scrolling with jQuery?jQueryHow to show slider value in HTML5 range input using JavaScript?HTMLHow to render lists inside a component in React?ReactHow to display a student’s individual transcript in Colleague?ColleagueUse inline if to make a shorter conditional syntax in ReactReactJavaScript String Properties and MethodsJavaScript