Swift 4: substring (c :) "deprecated: use a string scan index

I am using the decoding function for html. But I get this warning. How can I get rid?

func decode(_ entity : String) -> Character? {

    if entity.hasPrefix("&#x") || entity.hasPrefix("&#X"){
        return decodeNumeric(entity.substring(with: entity.index(entity.startIndex, offsetBy: 3) ..< entity.index(entity.endIndex, offsetBy: -1)), base: 16)
    } else if entity.hasPrefix("&#") {
        return decodeNumeric(entity.substring(with: entity.index(entity.startIndex, offsetBy: 2) ..< entity.index(entity.endIndex, offsetBy: -1)), base: 10)
    } else {
        return characterEntities[entity]
    }
}

Thank.

+4
source share
1 answer

someString.substring(with: someRange)should be someString[someRange].

So change:

entity.substring(with: entity.index(entity.startIndex, offsetBy: 3) ..< entity.index(entity.endIndex, offsetBy: -1))

with

entity[entity.index(entity.startIndex, offsetBy: 3) ..< entity.index(entity.endIndex, offsetBy: -1)]

In other words, change the value .substring(with:to [and change the closing value )to ].

The result is Substring, not String. Therefore, you may need to wrap the result in String( )to get Stringsubstrings from the result.

+15
source

Source: https://habr.com/ru/post/1687498/


All Articles