split is a method in an extension of CollectionType which, as of Swift 2, String no longer conforms to. Fortunately there are other ways to split a String:
-
Use
componentsSeparatedByString:"ab cd".componentsSeparatedByString(" ") // ["ab", "cd"]As pointed out by @dawg, this requires you
import Foundation. -
Instead of calling
spliton aString, you could use the characters of theString. Thecharactersproperty returns aString.CharacterView, which conforms toCollectionType:"😀 🇬🇧".characters.split(" ").map(String.init) // ["😀", "🇬🇧"] -
Make
Stringconform toCollectionType:extension String : CollectionType {} "w,x,y,z".split(",") // ["w", "x", "y", "z"]Although, since Apple made a decision to remove
String‘s conformance toCollectionTypeit seems more sensible to stick with options one or two.
In Swift 3, in options 1 and 2 respectively:
componentsSeparatedByString(:)has been renamed tocomponents(separatedBy:).split(:)has been renamed tosplit(separator:).