将 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/