我是新來這個網站,請裸露在我身邊。 我想在Swift中構建一個簡單的計算器,但我不知道如何或爲什麼我得到錯誤(「字符串類型的值沒有成員類型Int」)或如何解決它。這是到目前爲止我的代碼:Swift代碼錯誤:類型字符串的值有沒有成員Int
class ViewController: UIViewController {
var isTypingNumber = false
var firstNumber = Int!()
var secondNumber = Int!()
var operation = ""
@IBOutlet weak var calculatorDisplay: UILabel!
@IBAction func acButtonTapped(sender: AnyObject) {
}
@IBAction func number7Tapped(sender: AnyObject) {
let number7 = sender.currentTitle
if isTypingNumber{
calculatorDisplay.text = calculatorDisplay.text! + number7!!
}else{
calculatorDisplay.text = number7
isTypingNumber = true
}
}
@IBAction func divideTapped(sender: AnyObject) {
isTypingNumber = false
firstNumber = calculatorDisplay.text?.Int()! **//Error: Value of type 'String' has no member 'Int'**
operation = sender.currentTitle!!
}
@IBAction func equalsTapped(sender: AnyObject) {
isTypingNumber = false
var result = 0
secondNumber = calculatorDisplay.text?.Int()! **//Error: Value of type 'String' has no member 'Int'**
if operation == "+" {
result = firstNumber + secondNumber
} else if operation == "-" {
result = firstNumber - secondNumber
}else if operation == "X" {
result = firstNumber * secondNumber
}else if operation == "÷"{
result = firstNumber/secondNumber
}
calculatorDisplay.text = "\(result)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
firstNumber = 0
secondNumber = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
哪兒我去錯了嗎?
不要濫用武力展開操作符(''!)這樣的。 – Alexander