8

I am using a decode function for html. But I am getting this warning. How can I get rid off?

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 you.

user1046037
  • 16,755
  • 12
  • 92
  • 138
Mayday
  • 187
  • 2
  • 8
  • Have you Googled `substring(with:)' is deprecated: Please use String slicing subscript`? There seems to be a lot there – Pekka Oct 14 '17 at 21:43
  • Yeah I searched off course. But I didnt find works answer for me. And I didn't understand why I am getting this error. – Mayday Oct 14 '17 at 21:44

1 Answers1

25

The someString.substring(with: someRange) just needs to 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 .substring(with: to [ and change the closing ) to ].

The result is a Substring, not String. So you may need to wrap the result in String( ) to get a String from the substring result.

rmaddy
  • 314,917
  • 42
  • 532
  • 579