我有将数字存储在Label数组中的代码。
@IBAction func MSSave(_ sender: UIButton) {
MSarrayResults.removeAll()
if let MSText = displayResultLabel.text {
let result = "\(MSText)"
MSarrayResults.append(result as AnyObject)
print ("\(MSText)")
}
}
在屏幕上显示数组的按钮(displayResultLabel)
@IBAction func MRRead(_ sender: UIButton) {
displayResultLabel.text = "\(MSarrayResults)"
}
但是当我将数组插入到 Label 括号中时会出现(Look Photo)。
你在做什么:
displayResultLabel.text = "\(MSarrayResults)"
正在将文本设置为数组的“默认字符串表示”。这通常看起来像这样(如果数组中有 5 个数字):
[5, 7, 85, 13, 9]
或者,如您所见:
[85]
如果数组中只有一个数字。
从本质上讲,它是一种“人类可读”的表示。
如果要将文本设置为数组中的第一个元素,可以使用:
displayResultLabel.text = "\(MSarrayResults.first)"
或者:
displayResultLabel.text = "\(MSarrayResults[0])"
但是,由于您的MSSave()
函数在附加新字符串之前删除了所有元素MSArrayResults
,因此当您可以简单地使用 String 变量时,使用数组没有任何意义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句