Is there a cin equivalent in Swift?

I teach an iOS app development class at a community college. i am switching to Swift this year from Objectiv-C. The students have taken at least one programming course before, usually C++ but possibly Java or VIsual Basic. To start them off with a problem they can understand how to solve in C++, I give them an assignment which requires console input.


This turns out to be not so simple. Is there an easy, or recommended way to do this in Swift? I am starting them off with a MacOS console project. Not using Swift playgrounds.


If there is no simple way to obtain this service...how does one go about submitting a request to have it added to the language?


Best regards, and thank you all in advance for your replies.

There are not strict equivalent to `cin` of C++ in Swift. You can call C-standard library from Swift and you can use `stdin`, but this may not be what you want.


If your purpose is just handling a simple input from console, and handling each data byte by byte is out of your scope, a global function `readLine()` would be the simplest way.


import Foundation
print("Hello, World!")
while let line = readLine() {
    let sum = line.components(separatedBy: ",")
        .map{Int($0.trimmingCharacters(in: .whitespaces)) ?? 0}
        .reduce(0, +)
    print("sum=\(sum)")
}


Hello, World!

1,2,3

sum=6

Program ended with exit code: 0

Is there a cin equivalent in Swift?
 
 
Q