Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

swift - why Xcode didn't make addition?

I use gm stepper in my app and , it corresponding to labels. I have 4 different labels and one additional label for addition to values . The labels which corresponding to GM Steppers working well but ? stumble in addition values of labels.

class ViewController: UIViewController {

@IBAction func gmstp1(_ sender: GMStepper) {
    label1.text = String(sender.value*1.5)
    label6.text = String(sender.value)
}

@IBOutlet weak var label1: UILabel!


@IBAction func gmstp2(_ sender: GMStepper) {
    label2.text = String(sender.value*0.89)

}
@IBOutlet weak var label2: UILabel!


@IBAction func gmstp3(_ sender: GMStepper) {
    label3.text = String(sender.value*26)

}
@IBOutlet weak var label3: UILabel!


@IBAction func gmstp4(_ sender: GMStepper) {
    label4.text = String(sender.value*4)


}
@IBOutlet weak var label4: UILabel!

@IBOutlet weak var label5: UILabel!

My question about ; Is there any solution without using buttons? Could you handle that?

*Label5 using for summary.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simply because, unlike a GMStepper, a UILabel doesn't have a value property. So you'll have to get the text string in each label, convert it to a Double, and then add them up:

if let text1 = label1.text, let value1 = Double(text1),
    let text2 = label2.text, let value2 = Double(text2),
    let text3 = label3.text, let value3 = Double(text3),
    let text4 = label4.text, let value4 = Double(text4) {
    let sum = value1 + value2 + value3 + value4
    label5.text = String(sum)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...