Mike Strong Mike Strong - 1 year ago 257
Swift Question

how do I properly express this 'rangeOfCharacter' statement using swift 3?

I have some logic that allows me to listen to the editing of a textfield for invalid characters from a character set I created, obviously do to the rearrangement in swift 3 syntax, I get the following error:

Cannot invoke initializer for type 'Range<Index>' with an argument list of type '(DefaultBidirectionalIndices<String.CharacterView>)

on this line of code:

if let _ = string.rangeOfCharacter(from: invalidCharacters, options: [], range:Range<String.Index>(string.characters.indices))

I've looked into the new API doc's but can't seem to find a correct formatting for this line in swift 3... any suggestions?

Answer Source

You just need to use ..< operator to create your range. Note: If you want to check the whole string you can omit the options and range parameters.

if let _ = string.rangeOfCharacter(from: invalidCharacters, options: [], range: string.startIndex..<string.endIndex) {


Or simply:

if let _ = string.rangeOfCharacter(from: invalidCharacters) {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download