我正在创建一个简单的计算器应用程序,目前正在努力删除我的按钮被点击时的最后一个字符.我正在使用dropLast()方法,但我一直收到错误
Missing Argument for parameter #1 in call
@IBAction func onDelPressed (button: UIButton!) { runningNumber = runningNumber.characters.dropLast() currentLbl.text = runningNumber }
解决方法
斯威夫特4(附录)
在Swift中,您可以在String实例上直接应用dropLast(),不再调用.characters来访问String的CharacterView:
var runningNumber = "12345" runningNumber = String(runningNumber.dropLast()) print(runningNumber) // 1234
Swift 3(原始答案)
我假设runningNumber是一个String实例.在这种情况下,runningNumber.characters.dropLast()不是String类型,而是CharacterView:
let foo = runningNumber.characters.dropLast() print(type(of: foo)) // CharacterView
在将String实例分配回String类型的属性之前,需要使用CharacterView实例化String实例,例如,
var runningNumber = "12345" runningNumber = String(runningNumber.characters.dropLast()) print(runningNumber) // 1234
就是你的情况
@IBAction func onDelPressed (button: UIButton!) { runningNumber = String(runningNumber.characters.dropLast()) currentLbl.text = runningNumber }