Counting words from standard input in Swift
Regarding standard input, see Martin's answer below.

As for splitting strings into words and returning them as a sorted dictionary with word counts, that sounds like a useful thing to have around as a String extension. This is simple to implement unless you insist on pure Standard Library solution:

import Foundation

public extension String {

    public var wordCounts: [String:Int] {
        var d: [String:Int] = [:]
        enumerateSubstringsInRange(characters.indices, options: .ByWords) { word, _, _, _ in
            guard let word = word?.lowercaseString else { return }
            d[word] = (d[word] ?? 0) + 1
        }
        return d
    }

    public var sortedWordCounts: [(word: String, count: Int)] {
        return wordCounts.sort{ $0.0 &lt; $1.0 }.map{ (word: $0, count: $1) }
    }
}


Usage as follows (to produce the same output as your code):

if let input = readLine() {
    let report = input.sortedWordCounts.map{"\($0) \($1)"}.joinWithSeparator("\n")
    print(report)
}


For example, the following input ↓

Baa, baa, black sheep,/ Have you any wool?/ Yes, sir, yes, sir,/ Three bags full


...yields the following output ↓

any 1
baa 2
bags 1
black 1
full 1
have 1
sheep 1
sir 2
three 1
wool 1
yes 2
you 1


Edit

As Martin points out in the comment, your Ruby code reads multiple lines, or even multiple files… Here is a version that would do something like that, though you’ll of course need to adapt it to your needs. Note that I’ve added that eof so that we can play with this in the Xcode.

public func += &lt;T&gt; (inout lhs: [T:Int], rhs: [T:Int]) {
    for (k, i) in rhs {
        lhs[k] = (lhs[k] ?? 0) + i
    }
}

print("Type your terminating token or just type return if an empty line works for you:")
let eof = readLine()
print("Enter your lines:")

var wordCounts: [String:Int] = [:]

while let line = readLine() where line != eof {
    wordCounts += line.wordCounts
}

let report = wordCounts
    .sort{ $0.0 &lt; $1.0 }
    .map{"\($0) \($1)"}
    .joinWithSeparator("\n")

print(report)