我有一个名为UILabel
的{{1}},最后一个索引中的元素是我想增加的数字。 nihoshim
是alpha
的集合。
UIButton
错误是:
初始化器'init(_ :)'要求'String.Index'符合'BinaryInteger'
答案 0 :(得分:1)
要获取字符串的最后一个字符,可以使用suffix()
str.suffix(1)
因此,在您的代码中,将let index = Int(liel.endIndex)
替换为
guard let index = Int(liel.suffix(1)) else {
return //or some error handling
}
答案 1 :(得分:0)
您不能仅将String.Index
转换为数字。
如果我们谈论的是endIndex
,最简单的方法是:
let index = liel.count
对于其他索引,这将是:
let string = "abcd"
let stringIndex: String.Index = string.endIndex
let index: Int = string.distance(from: string.startIndex, to: stringIndex)
答案 2 :(得分:0)
正如Sulthan所说,您可以使用距离如下所示
@IBAction func wordChoser(_ sender: UIButton) {
guard let liel = nihoshim.text, let morning = gameLabel.text else {
return
}
let tag: Int = sender.tag - 1
let index: Int = liel.distance(from: liel.startIndex, to: liel.endIndex)
var alephbet = ["א","ב","ג","ד","ה","ו","ז","ח","ט","י","כ","ל","מ","נ","ס","ע","פ","צ","ק","ר","ש","ת"]
if morning.count <= 3 {
gameLabel.text = morning + alephbet[tag]
gameLabel.text = morning + "\(index + tag)"
sender.isHidden = true
}
}