## The whitespace issue
Generally, people reinvent this problem and bad solutions over and over. Is this a space? " " and what about "\n", "\t" or some unicode whitespace character that you've never seen, in no small part because it is invisible. While you can get away with
### A weak solution
import Foundation
let pieces = "Mary had little lamb".componentsSeparatedByString(" ")
If you ever need to shake your grip on reality watch a WWDC video on strings or dates. In short, it is almost always better to allow Apple to solve this kind of mundane task.
## Robust Solution: Use NSCharacterSet
The way to do this correctly, IMHO, is to use `NSCharacterSet` since as stated earlier your whitespace might not be what you expect and Apple has provided a whitespace character set. To explore the various provided character sets check out Apple's [NSCharacterSet developer documentation][1] and then, only then, augment or construct a new character set if it doesn't fit your needs.
### NSCharacterSet whitespaces
> Returns a character set containing the characters in Unicode General
> Category Zs and CHARACTER TABULATION (U+0009).
let longerString: String = "This is a test of the character set splitting system"
let components = longerString.components(separatedBy: .whitespaces)
print(components)
[1]:
[To see links please register here]