2015-05-21 19 views
10

Quando la decodifica risposta JSON dal webservice ottengo un errore che dice:Impossibile lanciare valore di tipo '__NSArrayM' (0x34df0900) per 'NSDictionary' SWIFT

Could not cast value of type '__NSArrayM' (0x34df0900) to 'NSDictionary' 

ho provato tante soluzioni trovate anche in StackOverflow, ma niente funziona.

My Code:

let jsonData:NSDictionary = (NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as? NSDictionary)! 

let success:NSInteger = jsonData.valueForKey("success") as! NSInteger 

risposta dal servizio Web:

[ 
    { 
     "id": "1", 
     "title": "bmw", 
     "price": "500.00", 
     "description": "330", 
     "addedDate": "2015-05-18 00:00:00", 
     "user_id": "1", 
     "user_name": "CANOVAS", 
     "user_zipCode": "32767", 
     "category_id": "1", 
     "category_label": "VEHICULES", 
     "subcategory_id": "2", 
     "subcategory_label": "Motos", 
     "bdd": {} 
    } 
] 

Grazie per il vostro aiuto

risposta

12

Provare a sostituire seguente riga:

let jsonData:NSDictionary = (NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as? NSDictionary)! 

Con seguente:

let jsonData:NSArray = (NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as? NSArray)! 

Spero che questo vi aiuterà!

Cheers!

+0

io sono, non davanti al mio mac fino a stasera, ti dirò che stasera! Grazie per il vostro messaggio ! – f1rstsurf

+0

ora ho: "Operatore binario! = 'Non può essere applicato agli operandi di tipo' NSArray? ' "and 'nil' per" se jsonData ["message"] as? NSArray! = nil {" – f1rstsurf

+0

come usare qualcosa come' NSArray.count> 0' come cosa per verificare se non è vuoto o vuoto? –

4

Uso SwiftyJSON: https://github.com/SwiftyJSON/SwiftyJSON

let json = JSON(data: urlData!) 

E se il successo è nella matrice

if let success = json[0]["success"].int { 
    //Now you got your value 
} 

O se il successo non è nella matrice

if let success = json["success"].int { 
    //Now you got your value 
} 

È inoltre possibile controllare il valore successo

if let success = json["success"].int where success == 1 { 
    // Now you can do stuff 
} 
3

Questo avverrà se si perde un "livello" che legge i registri. Ero incontrando questo errore, provato colata ad una NSArray anziché NSMutableDictionary come qui Swift JSON error, Could not cast value of type '__NSArrayM' (0x507b58) to 'NSDictionary' (0x507d74)

Il contenuto effettivo dell'oggetto erano all'interno di un NSDictionary all'indice 0 della matrice. Prova con questo codice (incluse alcune righe di log per illustrare)

 let dataDict = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &error) 

     println(dataDict) 

     let contents = dataDict!.objectForKey("rows") as! NSMutableArray 

     println(contents) 

     println("contents is = \(_stdlib_getDemangledTypeName(contents))") 

     let innerContents = contents[0] 

     println(innerContents) 

     println("inner contents is = \(_stdlib_getDemangledTypeName(innerContents))") 

     let yourKey = innerContents.objectForKey("yourKey") as? String 
     println(yourKey) 
Problemi correlati