i'm not sure if api in swift 2 changed, can't filter work on string in swift 2. following should change "abc123$$$ ff" "abcff".
// removes special characters , whitespaces func compressstring(astring: string) -> string{ let charset = nscharacterset.lettercharacterset() // following don't work: // return astring.filter{charset.contains($0)} // return string(filter(astring).{charset.contains($0)}) }
if want use filter, need run on characters view:
// removes special characters , whitespaces func compressstring(astring: string) -> string{ let charset: [character] = ["$", " ", "1", "2", "3"] // following don't work: return string(astring.characters.filter { !charset.contains($0) }) } let before = "abc123$$$ ff" let after = compressstring(before) // "abcff"
Comments
Post a Comment