私は、コードは1つの空のパラメータをチェックする必要がありますか?例えば数量と価格だけでなく、名前も...このコードを使ってどうすればいいですか?多くの空のパラメータを確認しますか?</p> <pre><code>if let isEmpty = name?.isEmpty where isEmpty == false { </code></pre> <p>が、私は多くの人が偽であるかどうかを確認するためのコードを実装する必要があります。
私はそれらのリストを置くとき、私はループの終わりにtryキャッチループを行うときに動作しません。
if let name = name, total = total, price = price, quantity = quantity
where !name.isEmpty && !total.isEmpty && !price.isEmpty && !quantity.isEmpty {
// use name, total, price, quantity
}
もう一つは、間違いなくより良い解決策は、1度に1つずつのラップを解除することである工程 - :
@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
ただ1つのパラメータが=になることはありますか? – Leanneheal