我的代碼檢查一個空的參數:檢查很多空的參數?
if let isEmpty = name?.isEmpty where isEmpty == false {
,但我需要實現的代碼來檢查,如果很多都是假的?例如數量和價格以及名稱......我將如何使用此代碼得到這方面的信息?
if let isEmpty = name?.isEmpty where isEmpty == false {
if let isEmpty = total?.isEmpty where isEmpty == false {
if let isEmpty = price?.isEmpty where isEmpty == false {
if let isEmpty = quantity?.isEmpty where isEmpty == false {
}
}
}
}
當我把它們的列表,它不起作用,當我在循環的末尾做一個try catch循環。
@IBAction func saveItems(sender: AnyObject) {
let name = txtName.text
let total = txtTotal.text
let price = txtPrice.text
let quantity = stepperValue.text
if let isEmpty = name?.isEmpty ||
isEmpty = price?.isEmpty ||
isEmpty = total?.isEmpty ||
isEmpty = quantity?.isEmpty where isEmpty == false {
}
// Create Entity
let entity = NSEntityDescription.entityForName("Item", inManagedObjectContext: self.managedObjectContext)
// Initialize Record
let record = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: self.managedObjectContext)
// Populate Record
record.setValue(txtName, forKey: "name")
record.setValue(txtTotal, forKey: "total")
record.setValue(txtPrice, forKey: "price")
record.setValue(stepperValue, forKey: "quantity")
record.setValue(NSDate(), forKey: "date")
do {
// Save Record
try record.managedObjectContext?.save()
// Dismiss View Controller
dismissViewControllerAnimated(true, completion: nil)
}
catch {
let saveError = error as NSError
print("\(saveError), \(saveError.userInfo)")
// Show Alert View
showAlertWithTitle(title: "Warning", message: "Your message could not be saved", cancelButtonTitle: "OK")
}
}
else {
// Show Alert View
showAlertWithTitle("Warning", message: "Your to-do needs a name.", cancelButtonTitle: "OK")
}
你究竟想要做什麼?你在最內層的「if」身上做了什麼?除了下一層嵌套之外,另一個「if」的內部是否還有其他內容? – nhgrif
我想在一個保存按鈕裏面實現這個代碼..他們都需要保存到核心數據,但我想這些沒有任何事情不會發生? – Leanneheal
只能有一個參數= false? – Leanneheal