Sum of 3 UITextField? [closed]

本小妞迷上赌 提交于 2019-12-04 02:42:56

问题


https://ibb.co/nH38wG

Why the result is 123 and not 6 ?

Anyone is able to elaborate and explain it to me please?


回答1:


You are adding the text (String) values to each other in the label text. You should do this instead:

guard let num1 = Int(textField1.text),
    let num2 = Int(textField2.text),
    let num3 = Int(textField3.text) else {
    return
}

let sum = num1 + num2 + num3
label.text = str+sum



回答2:


You are "summing" strings. "1"+"2"+"3"="123". Convert it to a number: Int("1")+Int("2")+Int("3")=6.

label.text = str + Int(textField1.text!)! + Int(textField2.text!)! + Int(textField3.text!)!


来源:https://stackoverflow.com/questions/46521131/sum-of-3-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!