2016-04-08 28 views
0

我的代碼檢查一個空的參數:檢查很多空的參數?

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") 
} 
+0

你究竟想要做什麼?你在最內層的「if」身上做了什麼?除了下一層嵌套之外,另一個「if」的內部是否還有其他內容? – nhgrif

+0

我想在一個保存按鈕裏面實現這個代碼..他們都需要保存到核心數據,但我想這些沒有任何事情不會發生? – Leanneheal

+0

只能有一個參數= false? – Leanneheal

回答

1

一種解決方案是一個很大的(但單一,沒有嵌套),複雜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, 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 

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 

當然,這取決於你在裏面是零或空與不中的情況下做什麼,第一個解決方案可能是更好的一個guard書面

如果我正在編寫此代碼,假設爲nil或爲空表示數據無法保存到數據庫的狀態,那麼我會選擇兩個guard之一謝謝,我選擇哪一個取決於如何描述我想要的消息給用戶。

0

您可以創建自己的isEmpty函數,它接受一個可選的字符串,如果字符串爲零或空,則返回true。然後,要一次檢查多個,您可以創建一組您想要檢查的字符串,並將其過濾爲清空。

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 
} 

你也可以讓自己的any功能上ArraySequenceType的擴展,它可能比過濾好。

+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