import UIKit
class ViewController: UIViewController {
@IBOutlet weak var Screen: UILabel!
var firstNumber = Float()
var secondNumber = Float()
var result = Float()
var operation = ""
var isTypingNumber = false
@IBAction func Number(sender: AnyObject) {
isTypingNumber = false
var number = sender.currentTitle
if isTypingNumber == true {
Screen.text = Screen.text! + number!!;
} else {
Screen.text = number!!;
}
isTypingNumber = true
}
@IBAction func Operation(sender: AnyObject) {
operation = sender.currentTitle!!
isTypingNumber = false
firstNumber = Screen.text!.toInt()!
}
@IBAction func Equals(sender: AnyObject) {
secondNumber = Screen.text!.toInt()!
isTypingNumber = false
if operation == "+" {
result = firstNumber + secondNumber;
} else if operation == "-" {
result = firstNumber - secondNumber;
} else if operation == "x" {
result = firstNumber * secondNumber;
} else if operation == "/" {
result = firstNumber / secondNumber;
}
Screen.text = "\(result)"
}
@IBAction func Clear(sender: AnyObject) {
firstNumber = 0
secondNumber = 0
result = 0
Screen.text = "\(result)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我的代码。我一直收到错误提示“Int”无法转换为“Float”,在firstNumber = Screen.text!.toInt()!和secondNumber = Screen.text!.toInt()!上。
我试图在UILabel(Screen)上显示浮点数。我应该怎么做? 有人能告诉我如何在UILabel中使用float,这是Swift。
谢谢, Rohit