Mike Strong
Mike Strong

Reputation: 940

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?

Upvotes: 2

Views: 2433

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236370

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) {

}

Upvotes: 6

Related Questions