erreur de programmation rapide erreur NSErrorPointer etc

var data: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: error) as NSDictionary; 

Cette ligne de code me donne une erreur

 NSError is not convertable to NSErrorPointer. 

J’ai alors pensé à changer le code pour:

 var data: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: &error) as NSDictionary; 

ce qui transformerait l’erreur NSError en un NSErrorPointer. Mais alors je reçois une nouvelle erreur et je ne peux pas la comprendre:

 NSError! is not a subtype of '@|value ST4' 

Ces types et méthodes ont beaucoup changé depuis Swift 1.

  1. Le préfixe NS est supprimé
  2. Les méthodes lancent maintenant des exceptions au lieu de prendre un pointeur d’erreur
  3. L’utilisation de NSDictionary est déconseillée. Utilisez plutôt un dictionnaire Swift

Cela se traduit par le code suivant:

 do { let object = try JSONSerialization.jsonObject( with: responseData, options: .allowFragments) if let dictionary = object as? [Ssortingng:Any] { // do something with the dictionary } else { print("Response data is not a dictionary") } } catch { print("Error parsing response data: \(error)") } 

De, si vous ne vous souciez pas de l’erreur d’parsing spécifique:

 let object = try JSONSerialization.jsonObject( with: responseData, options: .allowFragments) if let dictionary = object as? [Ssortingng:Any] { // do something with the dictionary } else { print("Response data is not a dictionary") } 

Réponse originale

Votre NSError doit être défini comme Optional car il peut être nul:

 var error: NSError? 

Vous souhaitez également prendre en compte le fait qu’il existe une erreur dans l’parsing qui renvoie nil ou l’parsing renvoyant un tableau. Pour ce faire, nous pouvons utiliser un casting optionnel avec le as? opérateur.

Cela nous laisse le code complet:

 var possibleData = NSJSONSerialization.JSONObjectWithData( responseData, options:NSJSONReadingOptions.AllowFragments, error: &error ) as? NSDictionary; if let actualError = error { println("An Error Occurred: \(actualError)") } else if let data = possibleData { // do something with the returned data } 

Comme mentionné, l’erreur doit être définie comme facultative ( https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/AdoptingCocoaDesignPatterns.html )

Cependant – Ce code CRASH si il y a une erreur et nil est retourné, le “As NSDictionary” serait le coupable:

 var data: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: &error) as NSDictionary; 

Vous devez faire l’parsing syntaxique json comme ceci:

 var jsonError : NSError? let jsonResult : AnyObject? = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &jsonError) if let error = jsonError{ println("error occurred: \(error.localizedDescription)") } else if let jsonDict = jsonResult as? NSDictionary{ println("json is dictionary \(jsonDict)") } else if let jsonArray = jsonResult as? NSArray{ println("json is an array: \(jsonArray)") } 

Ça marchera. Rappelez-vous également que json peut revenir en tant que tableau. Au lieu de passer à zéro pour les options, vous pouvez passer ce que vous voulez, par exemple:

 NSJSONReadingOptions.AllowFragments 

si tu veux.

Maintenant en beta-3

 var error: AutoreleasingUnsafePointer = nil var data: NSDictionary = NSJSONSerialization.JSONObjectWithData( w, options:.AllowFragments, error: error ) as NSDictionary; 

Vérifiez avec le code ci-dessous:

 var error: AutoreleasingUnsafePointer=nil var dataVal: NSData = NSURLConnection.sendSynchronousRequest(request1, returningResponse: response, error:nil) var err: NSError? var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary println("Result\(jsonResult)") 

Essayez d’utiliser

  var error: AutoreleasingUnsafePointer=nil