I'm trying to write a slugging function which involves stripping out any punctuation characters except for hyphens. I thought the best way to do this would be to create a new CharacterSet
as follows:
import Foundation
extension CharacterSet {
func subtracting(charactersIn string: String) -> CharacterSet {
let unwantedCharacters = CharacterSet(charactersIn: string)
return self.subtracting(unwantedCharacters)
}
}
let punctuationCharactersExcludingHyphen = CharacterSet.punctuationCharacters.subtracting(charactersIn: "-")
<#slug function using punctuationCharactersExcludingHyphen#>
where slug function
is a function that I've already tested with existing character sets. The problem is that the assignment let punctuationCharactersExcludingHyphen...
crashes with a EXC_BAD_ACCESS code=2
.
I've noticed that most problems involving this error are caused by some specific syntax error or the like, but I can't find out what it is here. Any ideas?