Igy Igy - 6 months ago 26
Swift Question

Swift - remove last typed digit from uilabel

i have calculator from stanford swift course, and have a task if user type numbers in label, and if it is a mistake, user can remove last typed digit from uilabel, i already put button for removing last digit from label, but code does not work, help, thnx.

@IBAction func removeLastDigit(sender: UIButton) {

let name: String = self.display.text!
let stringLength = count(name)
let substringIndex = stringLength - 1
name.substringToIndex(advance(name.startIndex, substringIndex))



}

Answer

Try this,

@IBAction func removeLastDigit(sender: UIButton) {

    let name: String = self.display.text!
    let stringLength = count(name)
    let substringIndex = stringLength - 1
    yourlabel.text = (name as NSString).substringToIndex(substringIndex)
}
Comments