2017-10-14 31 views
1

我正在使用html的解码函数。但是我收到了这个警告。我怎样才能摆脱?Swift 4:substring(with :)'已弃用:请使用String切片下标

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] 
    } 
} 

谢谢。

+0

你是否谷歌搜索'substring(with :)'已弃用:请使用字符串切片下标'?那里似乎有很多 –

+0

是的,我偏离了过去。但我没有找到适合我的作品。我不明白为什么我得到这个错误。 – Mayday

回答

13

someString.substring(with: someRange)只需要someString[someRange]

因此改变:

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

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

换句话说,改变.substring(with:[,改变收盘)]

结果是Substring,而不是String。因此,您可能需要将结果打包到String()以从子字符串结果中获得String

相关问题