gpt4 book ai didi

swift - 检查许多空参数?

转载 作者:行者123 更新时间:2023-11-30 13:31:45 25 4
gpt4 key购买 nike

我有代码来检查一个空参数:

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 let name = name, total = total, price = price, quantity = quantity
where !name.isEmpty && !total.isEmpty && !price.isEmpty && !quantity.isEmpty {

// use name, total, price, quantity

}
<小时/>

另一种可以说是更好的解决方案是一次一个地解开它们,一步一步地为每个变量使用一个 guard 语句。

guard let name = name where !name.isEmpty else {
// name is nil or empty
return
}

guard let total = total where !total.isEmpty else {
// total is nil or empty
return
}

guard let price = price where !price.isEmpty else {
// price is nil or empty
return
}

guard let quantity = quantity where !quantity.isEmpty else {
// quantity is nil or empty
return
}

// use name, total, price, and quantity, all are non-nil, non-empty
<小时/>

当然,根据您在它们为 nil 或为空或不为空的情况下所做的操作,第一个解决方案可能最好使用 guard 编写:

guard let name = name, total = total, price = price, quantity = quantity
where !name.isEmpty && !total.isEmpty && !price.isEmpty && !quantity.isEmpty else {

// something was nil or empty
return

}

// use name, total, price, quantity
<小时/>

如果我正在编写这段代码,假设nil或为空表示数据无法保存到数据库的状态,我会选择两个guard之一 方法,我选择哪种方法取决于我希望向用户传达的消息的描述性。

关于swift - 检查许多空参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36499543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com