swift - 由于 'internal' 保护级别,“indexOf”无法访问

标签 swift string swift3 indexof

将 Swift 2.0 迁移到 3.0 之后。我有一些错误,我遇到了问题:“由于‘内部’保护级别,‘indexOf’在 Swift 3 上无法访问”:

//代码

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

最佳答案

像这样尝试。

选项 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"]

关于swift - 由于 'internal' 保护级别,“indexOf”无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39894045/

相关文章:

ios - 如何找出 NSData 数组的长度?

c - 我在使用字符串通过引用传递时遇到问题

ios - 如何在 MSStickerBrowserViewController 中设置 stickerSize

ios - 如何在 uitextfield 下使用 uitableview 内部错误并验证

arrays - 如何迭代没有 id 属性的对象列表

swift - 在扩展名中打印文件名

用于检查字符串的正则表达式是否为一定长度

arrays - swift 3 : Sort Ordered List of Objects by Property

ios - Swift 3 设备运动重力

java - 如何使用 split() 正确分割文件名的字符串输入?