Skip to content

Instantly share code, notes, and snippets.

@erica
Last active September 11, 2015 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/5cd0943b2ba958b2aa69 to your computer and use it in GitHub Desktop.
Save erica/5cd0943b2ba958b2aa69 to your computer and use it in GitHub Desktop.
// Split
extension String {
public func split(
maxSplit: Int = .max,
allowEmptySlices: Bool = false,
@noescape isSeparator: ((Character) throws -> Bool)) rethrows -> [String] {
return try self.characters.split(
maxSplit,
allowEmptySlices: allowEmptySlices,
isSeparator: isSeparator)
.map({String($0)})
}
public enum StringSplitError : ErrorType {case MissingCharacter}
public func split(characterString: String) throws -> [String] {
guard let c = characterString.characters.first else {throw StringSplitError.MissingCharacter}
return self.split{$0 == c}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment