2016-02-26 117 views
2

如何減少這種(下面)情況下if語句的數量?我知道開關語句,但無法想象這將如何幫助在這種情況下。減少嵌套If-語句的數量

這裏發生的事情如下:

  1. 第一IF安全檢查存儲在NSDefault日期,可如果日期存在(真)或可能不存在
  2. ,它會檢查日期在「現在」之前
  3. 如果日期在「now」(真)之前,它安全地檢查NSDefault中的開/關用戶偏好,這也可能不存在
  4. 如果確實存在,如果它不等於關閉
  5. 最後,如果#4是真實的,則該函數被稱爲

    //Queue up more quote notifications? 
    if let notif_EndDate = (userPref_NSDefault.objectForKey("notification_EndDate")) as? NSDate { 
        if notif_EndDate.isLessThanDate(self.now) { 
         if let quoteNotif_Pref = (userPref_NSDefault.stringForKey("WeeklyQuote_Pref")) { 
          if quoteNotif_Pref != "Off" { 
           quoteNotifications() 
          } else { 
           print("Use has set 'Weekly Quotes' to 'Off'") 
          } 
         } else { 
          quoteNotifications() 
         } 
        } 
    } else { 
        quoteNotifications() 
    } 
    

回答

4

您可以使用模式在switch語句匹配:

switch (userPref_NSDefault.objectForKey("notification_EndDate"), userPref_NSDefault.stringForKey("WeeklyQuote_Pref")) 
{ 
    case let (date as NSDate, weeklyPref) where date.compare(NSDate()) == .OrderedAscending && weeklyPref == "Off": 
     print("User has set 'Weekly Quotes' to 'Off'") 
    default: 
     quoteNotifications() 
} 

你也可以連接您的可選綁定(基於@Patrick斯蒂芬回答)

if let notif_EndDate = userPref_NSDefault.objectForKey("notification_EndDate") as? NSDate, 
    quoteNotif_Pref = userPref_NSDefault.stringForKey("WeeklyQuote_Pref") as? String 
{ 
    if notif_EndDate.isLessThanDate(self.now) && quoteNotif_Pref == "Off" 
    { 
     print("Use has set 'Weekly Quotes' to 'Off'") 
     return 
    } 
} 
quoteNotifications() 
+1

這是sooo .... swifty。仍然在辯論是否對我來說太聰明瞭。 :) – Eiko

+0

在你的第一個代碼示例(開關)中,如果兩個NSDefault對象都不存在,會發生什麼?這是否會導致錯誤,或者它會運行「default:」? –

+1

默認運行,因爲模式不匹配(日期不是NSDate或weeklyPref不是字符串) – nielsbot

1

第一步驟將涉及不重複quoteNotifications()調用。

它不會減少if語句,但會減少else語句。

//Queue up more quote notifications? 
if let notif_EndDate = (userPref_NSDefault.objectForKey("notification_EndDate")) as? NSDate { 
    if notif_EndDate.isLessThanDate(self.now) { 
     if let quoteNotif_Pref = (userPref_NSDefault.stringForKey("WeeklyQuote_Pref")) { 
      if quoteNotif_Pref == "Off" { 
       print("Use has set 'Weekly Quotes' to 'Off'") 
       return 
      } 
     } 
    } 
} 
quoteNotifications() 
4

可以使用guard聲明:

//Queue up more quote notifications? 
guard let notif_EndDate = (userPref_NSDefault.objectForKey("notification_EndDate")) as? NSDate 
    where notif_EndDate.isLessThanDate(self.now) else { 
    quoteNotifications() 
    return 
} 
guard let quoteNotif_Pref = (userPref_NSDefault.stringForKey("WeeklyQuote_Pref")) 
    where if quoteNotif_Pref != "Off" else { 
    quoteNotifications() 
    return 
} 
print("Use has set 'Weekly Quotes' to 'Off'") 

注:這是你的代碼的直接翻譯,但它不是我會怎樣設計它。 guard聲明用於檢查範圍是否符合特定條件,如果不符合,則提前返回。理想情況下,您最終可以撥打queueNotifications()

1

你可以改變你的if-let報表guard聲明

guard let notif_EndDate = (userPref_NSDefault.objectForKey("notification_EndDate")) as? NSDate else { quoteNotifications() } 

if notif_EndDate.isLessThanDate(self.now) { 
    guard let quoteNotif_Pref = (userPref_NSDefault.stringForKey("WeeklyQuote_Pref")) else { quoteNotifications() } 

    if quoteNotif_Pref != "Off" { 
     quoteNotifications() 
    } else { 
     print("Use has set 'Weekly Quotes' to 'Off'") 
    } 
}