There is a problem with your use of
let input = standardInput.availableData
to read the input. If standard input is a regular file then this will read the
entire file contents. But if standard input is a tty (e.g. a Terminal window)
then it will just wait until a single line is entered and return.
You have to repeat the call until availableData returns an empty
data object. And if standard input is some other communications channel
(e.g. a pipe) then the only thing you know is that it returns at least
one byte (which may be a incomplete UTF-8 sequence).
There is already a function for that purpose: readLine()readLine() reads from
standard input and returns each line as a Swift String (or nil on EOF).
So your main loop would be:
while let line = readLine() {
// count words in `line` ...
}
Your isWordChar() function can be simplified to
func isWordChar (c: Character) -> Bool {
return "abcdefghijklmnopqrstuvwxyz'".characters.contains(c)
}
instead of converting the Character to String and searching
that as a substring. (But using enumerateSubstringsInRange()
instead of your own splitting function as suggested in @milos' answer
is probably the better way to go.)