将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)
}
}
}
}
答案 0 :(得分: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"]