title: Initializer for conditional binding must have Optional type, not 'AnyObject - Approach
body: <p>The following code throws a message which says "Initializer for conditional binding must have Optional type, not 'AnyObject'"</p>
  
  <pre><code>    func parseData2(){
          var data:NSData?
  
  
          if let data2 = data  {
              do {
                  let details = try NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)
  
                  if let actualDetails = details where actualDetails.isKindOfClass(NSDictionary) {
                      print("Parse Data")
                  }
  
              }catch {
                  print("Error \(error)")
              }
          }
  
      }
  </code></pre>
  
  <p>To resolve the above error I used the following code.</p>
  
  <pre><code>    func parseData2(){
          var data:NSData?
  
  
          if let data2 = data  {
              do {
                  let details:AnyObject = try NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)
  
                  if let actualDetails:AnyObject = details where actualDetails.isKindOfClass(NSDictionary) {
                      print("Parse Data")
                  }
  
              }catch {
                  print("Error \(error)")
              }
          }
  
      }
  </code></pre>
  
  <p>Is there any better approach then the above or my code might crash?</p>
  
  <p>There is one more code which I want to add considering <strong>nil check</strong>,<strong>type check</strong> and then <strong>type cast check</strong>. The reason behind that Swift offers great flexibility but litle bit difficult to fix issues. Let's say I have a dictionary, <strong>cityDetails</strong> and I am trying to get data for  self.cityZipCode and self.cityIdentifier, which are optional, defined as var cityZipCode:Int? and  var cityIdentifier:Int?</p>
  
  <pre><code>if let cityBasic = cityDetails["basicDetails"] where 
  cityBasic!.isKindOfClass(NSDictionary) {
  
  self.cityZipCode = (cityBasic as! NSDictionary)["zip"].integerValue ?? 0
  
  self.cityIdentifier =  (cityBasic as! NSDictionary)["cityId"].integerValue ?? 0
  
  }
  </code></pre>