title: How to use optional chaining while searching through a dictionary in swift?
body: <p>I want to safely search through values in a swift Dictionary using if lets and making sure it is type safe as I get deeper and deeper into the dictionary. The dictionary contains dictionaries that contains NSArray that contains more dictionary. </p>
  
  <p>At first attempt my code looks like this: </p>
  
  <pre><code>  if let kkbox = ticket["KKBOX"] as? Dictionary&lt;String, AnyObject&gt; {
          if let kkboxDlUrlDict = kkbox["kkbox_dl_url_list"] as? Dictionary&lt;String, AnyObject&gt; {
              if let kkboxDlUrlArray = kkboxDlUrlDict["kkbox_dl_url"] as? NSArray {
                  for dict in kkboxDlUrlArray {
                      if let name = dict["name"] as? String {
                          if name == mediaType.rawValue {
                              urlStr = dict["url"] as String
                          }
                      }
                  }
              } else { return nil }
          } else { return nil }
      } else { return nil }
  </code></pre>
  
  <p>How do I shorten it to perhaps one or 2 line? </p>
  
  <p>I realised I can chain it if it is 2 layers. This works: </p>
  
  <pre><code> if let kkboxDlUrlArray = ticket["KKBOX"]?["kkbox_dl_url_list"] as? NSArray {
  
      }
  </code></pre>
  
  <p>But any chain longer than that, will not compile. </p>
  
  <p>Is there a way to chain through a dictionary more than once? </p>
  
  <p>Thank you</p>