2014-06-12 6 views
42
var data: NSDictionary = 
    NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: error) as NSDictionary; 

Questa riga di codice mi dà errorerapida programmazione errore NSErrorPointer ecc

NSError is not convertable to NSErrorPointer. 

Così ho quindi pensato di modificare il codice a:

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

atta a rendere l'errore NSError in un NSErrorPointer. Ma poi ho un nuovo errore e non riesco a dare un senso a:

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

risposta

68

tuo NSError deve essere definito come un Optional perché può essere pari a zero:

var error: NSError? 

Si vuole rappresentare anche lì essere un errore nel parsing che restituirà nil o l'analisi che restituisce un array. Per fare ciò, possiamo usare un casting opzionale con l'operatore as?.

Questo ci lascia con il codice completo:

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 
} 
+3

Ciò in crash se v'è un errore di analisi del JSON e nullo viene restituito. Vedere questa risposta: http://stackoverflow.com/a/24333999/1687195 – user1687195

+0

Ho aggiornato la risposta per evitare arresti anomali da errori di analisi – drewag

0

Cheque Con codice qui sotto:

var error: AutoreleasingUnsafePointer<NSErrorPointer?>=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)") 

Provare a utilizzare

var error: AutoreleasingUnsafePointer<NSErrorPointer?>=nil 
11

Come detto, l'errore deve essere definito come optional (https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/AdoptingCocoaDesignPatterns.html)

Tuttavia - Questo codice andrà in crash se v'è un errore e nullo viene restituito, il "come NSDictionary" sarebbe il colpevole:

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

Hai bisogno di fare il JSON analisi in questo modo:

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)") 
} 

Che funzionerà. Ricorda anche che JSON può tornare come una matrice. Invece di passare nil per le opzioni puoi passare quello che vuoi, ad esempio:

NSJSONReadingOptions.AllowFragments 

se ti piace.

1

ora in beta-3

var error: AutoreleasingUnsafePointer<NSError?> = nil 

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

oppure errore 'var: NSErrorPointer? = nil' in breve – Snowman

+0

Grazie moby, '?' è inutile Quindi, var errore: NSErrorPointer = nil – Satachito

Problemi correlati