Vemonus Vemonus - 2 months ago 10
Swift Question

What is the new way to trim a String?

Prior to being forced to switch to Swift 3.0 syntax, the following line worked just fine:

return urlString.stringByTrimmingCharactersInSet(unwantedChars)


However, having updated to Swift 3.0, it now prevents me from building my app in Xcode, showing me the error message
Value of Type 'String' has no member 'StringByTrimmingCharactersInSet'
. What is the new syntax for this?

Answer

Swift3 inctroduced a lot of changes shortening api names and making them more 'swiftly'. Your code would look the following in swift3:

let st = s.trimmingCharacters(in: unwantedChars)
Comments