【问题标题】:Swift 4 : substring(with:)' is deprecated: Please use String slicing subscript [duplicate]Swift 4:不推荐使用 substring(with:)':请使用字符串切片下标 [重复]
【发布时间】:2017-10-14 21:41:51
【问题描述】:

我正在使用 html 的解码功能。但我收到了这个警告。如何摆脱?

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

谢谢。

【问题讨论】:

  • 你用谷歌搜索过substring(with:)' is deprecated: Please use String slicing subscript吗?那里似乎有很多
  • 是的,我偏离了方向搜索。但我没有找到适合我的答案。而且我不明白为什么会收到此错误。

标签: swift swift4


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 2023-03-28
    • 2018-02-05
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    相关资源
    最近更新 更多