2016-04-08 1 views
0

私は、コードは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") 
} 
+0

実際に何をしたいですか?最も内側の 'if'の体内で何をしていますか?次のネスティングのレイヤーの他に、他の 'if'のボディの中に何か起こっていることはありますか? – nhgrif

+0

私はこのコードを保存ボタンの中に実装しようとしています。それらはすべてコアデータに保存する必要がありますが、何も起こっていないと思いますか? – Leanneheal

+0

ただ1つのパラメータが=になることはありますか? – Leanneheal

答えて

1

一つの解決策は、複雑なif文(入れ子にすることなく、しかし、単一の)大きさ変数ごとに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または空であると仮定すると、データをデータベースに保存できない状態を表します。私は2つのうちの1つを選択しますguard approa私が選んだチェスは、メッセージをユーザに伝えたいと思っているかどうかによって決まります。

0

オプションのStringを使用し、文字列がnilまたは空の場合はtrueを返す独自のisEmpty関数を作成できます。それから、一度に多数をチェックするには、チェックしようとしていた文字列を空にしてフィルタリングすることができます。

let arr = [name, total, price, quantity] 

let isEmpty = { (string: String?) -> Bool in 
    guard let string = string where !string.isEmpty else { return true } 
    return false 
} 

// if none of the strings are empty 
if arr.filter(isEmpty).isEmpty { 
    // do stuff 
} 

また、フィルタリングよりも良いかもしれませんArrayまたはSequenceType上の拡張機能として、独自のany機能を作ることができます。

+0

これより簡単なアプローチがありますが、ここでの最大の問題は、最終的には実際にはアンラップされた値を使用することです。あなたのアプローチを取る人は、おそらく、このアプローチでチェックした後、すべての値をアンラップすることです。 – nhgrif

+0

それは公正ですが、オプションをアンラップする同様のアプローチを使用することもできます。元の質問はちょうどisEmptyをチェックしていた。 –

-1
required_params = array('name','price',..)//all the keys required. 
input_params = array('name'=>'abc', 'price'=>20,...)//input array in which conditions are to be checked. 
foreach(required_params as $key_required){ 
    if(input_params[$key_required] is empty?) 
    return false; 
// if you dont have input_params array and have variables. just check 
//${$key_required} is empty 
    ..... 
} 
+0

??どうしましたか? – kanchan