2017-10-14 35 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

相關問題