我在swift中構建了一個計算空間區域的簡單應用程序。如果用戶未在文本框中輸入寬度或高度,則條件語句會出現問題,並返回一條消息。Swift中的條件語句
//
// ViewController.swift
// areaCalculator
//
//
// Copyright (c) 2014 Dandre Ealy. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var widthTxt: UITextField!
@IBOutlet weak var heightTxt: UITextField!
@IBOutlet weak var area: UILabel!
@IBAction func buttonPressed(sender: AnyObject) {
var width = widthTxt.text.toInt()
var height = heightTxt.text.toInt()
var areaPressed = width! * height!
if ((width) && (height) != nil){
area.text = "The area is \(areaPressed)"
} else {
area.text = "Enter the width and the height"
}
}
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.
}
}
你有什麼問題?從您的問題中不清楚 – jrturton
我收到&&邏輯運算符的錯誤,它表示「可選類型'$ T6'不能用作布爾值;測試'!= nil'而不是 – Swifter