【问题标题】:Value of type 'String' has no member indices'String' 类型的值没有成员索引
【发布时间】:2017-02-14 21:48:51
【问题描述】:

我正在尝试将我的 Swift 2.2 代码迁移到 Swift 3(beta),在迁移到 Swift 3.0 后,我在下面的代码中收到以下错误,“'String' 类型的值没有成员索引”

如果routePathComponent 是一个字符串,则需要帮助。

迁移后

if routePathComponent.hasPrefix(":") {
   let variableKey = routePathComponent.substring(with: routePathComponent.indices.suffix(from: routePathComponent.characters.index(routePathComponent.startIndex, offsetBy: 1)))
}

迁移前

if routePathComponent.hasPrefix(":") {
                            let variableKey = routePathComponent.substringWithRange(routePathComponent.startIndex.advancedBy(1)..<routePathComponent.endIndex)
                            let variableValue = component.URLDecodedString()
                            if variableKey.characters.count > 0 && variableValue.characters.count > 0 {
                                variables[variableKey] = variableValue
                            }
                        }

【问题讨论】:

    标签: swift xcode string swift3


    【解决方案1】:

    如果你想省略第一个字符,如果它是一个冒号,那就是 Swift 3 方式

    if routePathComponent.hasPrefix(":") {
      let variableKey = routePathComponent.substring(from: routePathComponent.index(after :routePathComponent.startIndex))
    }
    

    您在评论中的其他示例的等价物是

    if let endingQuoteRange = clazz.range(of:"\"") {
       clazz.removeSubrange(endingQuoteRange.upperBound..<clazz.endIndex)
       ...
    }
    

    【讨论】:

    • 感谢您的解决方案。让我试试您的解决方案。我正在尝试升级 2 年前编写的代码,我正在努力寻找问题中提出的上述代码的目的
    • 你能发布原始的 Swift 2.2 代码吗?问题中的代码似乎已经迁移了。
    • 是的,它确实切断了第一个字符,我的代码是 Swift 3 等效的
    • 一定会的。
    • rangeOf 返回字符串中引号的范围 (lowerBound..&lt;upperBound)。 lowerBound 是字符本身,upperBound 是引号后的第一个字符。 removeSubrange 删除引号 endingQuoteRange.upperBound 之后的所有内容,直到字符串 clazz.endIndex 结束。
    【解决方案2】:

    如果您只想从字符串中删除第一个字符,可以使用dropFirst()

    if routePathComponent.hasPrefix(":") {
        let variableKey = String(routePathComponent.characters.dropFirst())
    }
    

    【讨论】:

    • 您好,我在迁移之前已经粘贴了 swift 2.2 代码。我仍在努力寻找上述代码的用途。因为它的 2 年历史是由不再可用的开发人员编写的
    • @user578386 你有没有像我的回答一样尝试使用String(routePathComponent.characters.dropFirst())
    • 工作正常..我有类似的查询 if letendingQuoteRange = clazz.range(of: "\"") { clazz.removeSubrange((clazz.indices.suffix(from:endingQuoteRange.startIndex)) ) 返回 getClassNameFromFullClassName(clazz) }
    • @user578386 抱歉迟到了,但 Vadian 已经给了你想要的。
    【解决方案3】:

    indicesCollection 类型的属性。从 v.2.3 开始,String 不是 Collection

    使用.characters 属性,它返回CollectionString 的字符。

    【讨论】:

    • 我尝试使用 .characters 并收到以下错误“没有 'index' 候选产生预期的上下文结果类型 'Int'”。你能从你的最后检查上面的代码吗?可能出错了。我将不胜感激
    • 好吧,也许你应该为你的问题寻找不同的解决方案。您是否只需要删除初始“:”?为什么不直接使用String(string.characters.dropFirst()) 或类似的东西?
    • 以上代码是另一位开发人员编写的已有 2 年历史的代码,已不再可用,我无法理解上述代码的用途。
    【解决方案4】:
    var routePathComponent = ":Hello playground"
    
    
    if routePathComponent.hasPrefix(":") {
        let variableKey = routePathComponent.substringFromIndex(routePathComponent.startIndex.advancedBy(1))
        //print(variableKey)
    }
    

    这应该可以解决问题

    【讨论】:

    • 我尝试使用 .characters,但出现以下错误“No 'index' canditate generate the expected contextual result type 'Int'”。
    • advancedBy 在 Swift 3 中不可用
    猜你喜欢
    • 2016-05-27
    • 2016-12-03
    • 2019-08-21
    • 2019-02-03
    • 2016-06-08
    • 2018-07-12
    • 2018-03-14
    • 2019-07-25
    • 1970-01-01
    相关资源
    最近更新 更多