Complete Range Script on Swift 3.0

Asked

Viewed 37 times

0

I was normally using this code in a Swift 2.0 project, then decided to upgrade the project to version 3.0 and adapt some functions that changed... Only the last "squeak" argument I can’t adapt.

Code in Swift 2.0:

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

           return false
 }

Code on Swift 3.0:

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

        return false

 }

1 answer

2


You don’t need to pass the last argument if you want to check the whole string:

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

If Voce wants to pass the range you need to do it this way:

string.startIndex..<string.endIndex

In your case:

if let range = string.rangeOfCharacter(from: invalidCharacters, options: [], range: string.startIndex..<string.endIndex){
    print(range)
}
  • Thanks Leo, it worked out just fine!

  • You’re welcome Jadson.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.