Code Snippet: User Input-Single Line

From Coder Merlin
Revision as of 23:22, 3 December 2018 by Chukwuemeka-tinashe (talk | contribs) (Created page with "= User Input = Note: Input read from the console is terminated with a ^D (Control-D) which indicates the end-of-file. == Reading a single line of text from the console ==...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

User Input[edit]

Note: Input read from the console is terminated with a ^D (Control-D) which indicates the end-of-file.

Reading a single line of text from the console[edit]

// Read a single line of input from the console
let line = readLine()            // This will return an optional string
if line != nil {                 // Let's check if it's not nil, if so, it's really a string
    let text = line!             // Unwrap the string
    print("You typed: \(text)")  // Print the string
}