【问题标题】:'indexOf' is inaccessible due to 'internal' protection level由于“内部”保护级别,无法访问“indexOf”
【发布时间】:2016-10-06 10:51:21
【问题描述】:

在将 Swift 2.0 迁移到 3.0 之后。我有一些错误,我遇到了问题:Swift 3 上的“'indexOf' is inaccessible due to 'internal' protection level”:

// 代码

var indexIndexed = 0
                    for link in doc.css("li") {
                        if(link.className == "regular-search-result"){
                            for link2 in link.css("span") {
                                if(link2.className == "indexed-biz-name"){
                                    let num:String = link2.text!

                                    let lastPart = num.substring(to: num.index(num.startIndex, offsetBy: num.indexOf(".")!)) // Here

                                    print("le num est \(lastPart)")

                                    let numInt:Int = Int(lastPart)!

                                    if(numInt > 10 && numInt <= 20){
                                        indexIndexed = numInt - 10
                                    } else if(numInt > 20){
                                        indexIndexed = numInt - 20
                                    } else {
                                        indexIndexed = numInt
                                    }
                                    //print(indexIndexed)
                                }
                            }
                        }
                    }

【问题讨论】:

  • 在 Swift 3 中它现在是 index(of: ".")
  • 感谢您的回复。 @nirav-d 当我使用 index(of: ".") 时出现此错误:参数标签 '(of:)' 不匹配任何可用的重载
  • 你想要字符串的小数部分吗?例如“12.56”->你想要 56,对吧?
  • 是的,但我在这里使用 indexOf:let firstPart = link.innerHTML!.substringFrom(link.innerHTML!.startIndex.advancedBy(link.innerHTML!.index(of:"href=\"")!+6))
  • 您能在问题中添加清晰的示例吗?

标签: swift string swift3 indexof


【解决方案1】:

像这样试试。

选项 1

let mynum = "26.53"
if let range = mynum.range(of: ".") {
    let num1 = mynum.substring(to: mynum.index(range.lowerBound, offsetBy: 0)) // 26
    let num = mynum.substring(from: mynum.index(after: range.lowerBound)) // 53 
}

选项 2

let numArr = mynum.characters.split(separator: ".").map(String.init) // ["26","53"]

选项 3

let numArr = mynum.components(separatedBy: ".") // ["26","53"]

【讨论】:

    猜你喜欢
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 2017-12-23
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多