title: Xcode Swift: Could not cast value of type '__NSCFString' (0x102e8ac50) to 'NSDictionary' (0x102e8b8d0)
body: <p>I am trying to display my JSON information in the debug area but I am having a problem in which I have not found a solution.</p>
  
  <p>First of all, let me display the code I have so far:</p>
  
  <p><strong>BBUpJSON.swift</strong></p>
  
  <pre><code>import Foundation
  
  class BBUpJSON {
  
      func loadBbup(completion: ((AnyObject) -&gt; Void)!) {
  
          var urlString = "http://xxxdsgn.xxxhost.com/json/jnslst.json"
  
          let session = NSURLSession.sharedSession()
          let bbupUrl = NSURL(string: urlString)
  
          var task = session.dataTaskWithURL(bbupUrl!){
              (data, response, error) -&gt; Void in
  
              if error != nil {
                  println(error.localizedDescription)
              } else {
  
                  var error : NSError?
  
                  var bbupData = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &amp;error) as! NSArray
  
                  var jnslst = [JSONExtrct]()
  
                  for jnsextrct in bbupData{
                      let jnsextrct = JSONExtrct(data: jnsextrct as! NSDictionary)
                      jnslst.append(jnsextrct)
                  }
  
                  let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
                  dispatch_async(dispatch_get_global_queue(priority, 0)) {
                      dispatch_async(dispatch_get_main_queue()) {
                          completion(jnslst)
                      }
                  }
  
              }
          }
  
          task.resume()
      }
  
  }
  </code></pre>
  
  <p><strong>JSONExtrct.swift</strong></p>
  
  <pre><code>import Foundation
  
  class JSONExtrct {
  
      var titulo : String!
      var imageUrl : String!
      var imageData : NSData?
      var imageUrl2 : String!
      var imageData2 : NSData?
      var imageUrl3 : String!
      var imageData3 : NSData?
      var marca : String!
      var color : String!
      var tipo : String!
      var ref : Int!
  
      init(data : NSDictionary) {
  
          self.titulo = getStringFromJSON(data, key: "titulo")
  
          let image = data["image"] as! NSDictionary
          self.imageUrl = getStringFromJSON(image, key: "image")
  
          let image2 = data["image2"] as! NSDictionary
          self.imageUrl2 = getStringFromJSON(image, key: "image2")
  
          let image3 = data["image3"] as! NSDictionary
          self.imageUrl3 = getStringFromJSON(image, key: "image3")
  
          self.marca = getStringFromJSON(data, key: "marca")
  
          self.color = getStringFromJSON(data, key: "color")
  
          self.tipo = getStringFromJSON(data, key: "tipo")
  
          self.ref = data["ref"] as! Int
  
      }
  
      func getStringFromJSON(data: NSDictionary, key: String) -&gt;String{
  
          //let info : AnyObject? = data[key]
  
          if let info = data[key] as? String {
              return info
          }
  
          return ""
  
      }
  
  }
  </code></pre>
  
  <p><strong>ViewController.swift</strong></p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let json = BBUpJSON()
          json.loadBbup(nil)
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  
  }
  </code></pre>
  
  <p>So, when I run the app with a breakpoint in <code>for jnsextrct in bbupData{</code> in the <strong>BBUpJSON.swift</strong> file, it shows in the left side of the debug area that I have <strong>20 values</strong>. Everytime I run <code>po bbupData</code> in the right output of the debug area it just partially shows me 3 of the values out of the 20 I have in my JSON file. When I add another breakpoint in <code>completion(jnslst)</code> located at the end of the same swift file and press <strong>Continue program execution</strong>, I get the following error: <code>Could not cast value of type '__NSCFString' (0x10c3c2c50) to 'NSDictionary' (0x10c3c38d0)</code>. It then redirects the error to <code>let image = data["image"] as! NSDictionary</code> that is located in my <strong>JSONExtrct.swift</strong>.</p>
  
  <p>This is the <a href="http://wlodsgn.x10host.com/json/jnslst.json" rel="nofollow">link</a> to the json file that is trying to extract the information.</p>
  
  <p><strong>-----UPDATE------</strong></p>
  
  <p>Modified the values of the strings in <strong>JSONExtrct.swift</strong> to:</p>
  
  <pre><code>if let imageUrl = data["image"] as? String {
              self.imageUrl = imageUrl
          }
  
          if let imageUrl2 = data["image2"] as? String {
              self.imageUrl2 = imageUrl2
          }
  
          if let imageUrl3 = data["image3"] as? String {
              self.imageUrl3 = imageUrl3
          }
  </code></pre>
  
  <p>And added the same breakpoints to see the results in <strong>BBUpJSON.swift</strong>. From the breakpoint <code>for jnsextrct in bbupData{</code>, it still displays me the same 3 values out of the 20 I have (don't know if that should show that way) when I type <code>po bbupData</code> in the output area. When I <strong>Continue program execution</strong> to <code>completion(jnslst)</code>, it doesn't show me the previous error but it shows me the following output result when typing <code>po jnslst</code>:</p>
  
  <pre><code>(lldb) po jnslst
  [BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct, BonBonUp.JSONExtrct]
  </code></pre>
  
  <p>When I continue and add one more breakpoint at the last bracket, it shows me the following error: <code>fatal error: unexpectedly found nil while unwrapping an Optional value</code> and it redirects the error to <code>completion(jnslst)</code> showing <code>Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode=0x0)</code></p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p><a href="http://imgur.com/7Ef5GT5,Q745XYK#1" rel="nofollow">Breakpoint Line 1</a></p>
  
  <p><a href="http://imgur.com/7Ef5GT5,Q745XYK#0" rel="nofollow">Breakpoint Line 2</a></p>
  
  <p>It seems that it reads just one value through <code>jnsextrct</code> and returns it to <code>jnslst</code>. Then when I add the other breakpoint in the next screenshot it goes to the next value. When I type <code>po jnslst</code> in the second screenshot, it returns one <code>[BonBonUp.JSONExtrct]</code> instead of 20 of them like in the previous update.</p>
  
  <p><strong>UPDATE 3</strong></p>
  
  <p>Already added <code>CollectionView</code> to the <code>Main.StoryBoard</code> but I am getting the following errors when I try to upload JSON information (Not images yet) to each grid. Under <strong>JnsGridController.swift</strong>, in order to receive the information from my <code>JSON</code> files, I had to create a new method that would callback as <code>didLoadJns</code>. Here is the part of that code: </p>
  
  <pre><code>jns = [JsonExtrct]()
          let json = JnsJson()
          json.loadJns(didLoadJns)
      }
  
      func didLoadJns(jns: [JsonExtrct]){
          self.jns = jns
          collectionView.reloadData()
  </code></pre>
  
  <p>In <code>json.loadJns(didLoadJns)</code>, <code>didLoadJns</code> use to be <code>nil</code> before I added the <code>func</code> method but I am getting the following error: <code>Cannot invoke 'loadJns' with an argument list of type '(([JsonExtrct]) -&gt; ())'</code></p>
  
  <p>Also in <strong>JnsJson.swift</strong>, since I added that callback function in <strong>JnsGridController</strong> its going to be receiving arrays from <code>JsonExtrct</code> instead of <code>AnyObject</code> so I changed the function method from <code>func loadJns(completion: ((AnyObject) -&gt; Void)!) {</code> to <code>func loadJns(completion: ((JsonExtrct) -&gt; Void)!) {</code> and now I am getting a similar error as the one in <strong>JnsGridController</strong>: <code>Cannot invoke 'dispatch_async' with an argument list of type '(dispatch_queue_t!, () -&gt; _)'</code></p>