Complete Range Argument in Swift 3.0

0

I was normally using this code in a Swift 2.0 project, then I decided to update the project to version 3.0 and adapt some functions that have changed ... Only the last "range" argument I can not adapt.

Swift 2.0 code:

if let range = string.rangeOfCharacterFromSet(invalidCharacters, options: nil, range:Range<String.Index>(start: string.startIndex, end: string.endIndex)) {

           return false
 }

Code in Swift 3.0:

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

        return false

 }
    
asked by anonymous 26.07.2016 / 00:56

1 answer

2

You do not need to pass the last argument if you want to check the whole string:

return string.rangeOfCharacter(from: invalidCharacters) != nil

If you want to pass the range you need to do the following:

string.startIndex..<string.endIndex

In your case:

if let range = string.rangeOfCharacter(from: invalidCharacters, options: [], range: string.startIndex..<string.endIndex){
    print(range)
}
    
26.07.2016 / 06:11