mardi 4 août 2015

Xcode Swift: Could not cast value of type '__NSCFString' (0x102e8ac50) to 'NSDictionary' (0x102e8b8d0)

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.

First of all, let me display the code I have so far:

BBUpJSON.swift

import Foundation

class BBUpJSON {

    func loadBbup(completion: ((AnyObject) -> Void)!) {

        var urlString = "http://ift.tt/1T0gelg"

        let session = NSURLSession.sharedSession()
        let bbupUrl = NSURL(string: urlString)

        var task = session.dataTaskWithURL(bbupUrl!){
            (data, response, error) -> Void in

            if error != nil {
                println(error.localizedDescription)
            } else {

                var error : NSError?

                var bbupData = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &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()
    }

}

JSONExtrct.swift

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) ->String{

        //let info : AnyObject? = data[key]

        if let info = data[key] as? String {
            return info
        }

        return ""

    }

}

ViewController.swift

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.
    }


}

So, when I run the app with a breakpoint in for jnsextrct in bbupData{ in the BBUpJSON.swift file, it shows in the left side of the debug area that I have 20 values. Everytime I run po bbupData 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 completion(jnslst) located at the end of the same swift file and press Continue program execution, I get the following error: Could not cast value of type '__NSCFString' (0x10c3c2c50) to 'NSDictionary' (0x10c3c38d0). It then redirects the error to let image = data["image"] as! NSDictionary that is located in my JSONExtrct.swift.

This is the link to the json file that is trying to extract the information.

-----UPDATE------

Modified the values of the strings in JSONExtrct.swift to:

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
        }

And added the same breakpoints to see the results in BBUpJSON.swift. From the breakpoint for jnsextrct in bbupData{, 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 po bbupData in the output area. When I Continue program execution to completion(jnslst), it doesn't show me the previous error but it shows me the following output result when typing po jnslst:

(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]

When I continue and add one more breakpoint at the last bracket, it shows me the following error: fatal error: unexpectedly found nil while unwrapping an Optional value and it redirects the error to completion(jnslst) showing Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode=0x0)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire