var data: NSDictionary =
NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: error) as NSDictionary;
Diese Codezeile gibt mir Fehler
NSError is not convertable to NSErrorPointer.
Also überlegte ich mir, den Code zu ändern:
var data: NSDictionary =
NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: &error) as NSDictionary;
das würde den NSError-Fehler in einen NSErrorPointer verwandeln. Aber dann bekomme ich einen neuen Fehler und kann ihn nicht verstehen:
NSError! is not a subtype of '@|value ST4'
Diese Typen und Methoden haben sich seit Swift 1.
NS
wird gelöschtDaraus ergibt sich folgender Code:
do {
let object = try JSONSerialization.jsonObject(
with: responseData,
options: .allowFragments)
if let dictionary = object as? [String:Any] {
// do something with the dictionary
}
else {
print("Response data is not a dictionary")
}
}
catch {
print("Error parsing response data: \(error)")
}
Wenn Ihnen der spezifische Parsing-Fehler nicht wichtig ist:
let object = try JSONSerialization.jsonObject(
with: responseData,
options: .allowFragments)
if let dictionary = object as? [String:Any] {
// do something with the dictionary
}
else {
print("Response data is not a dictionary")
}
rsprüngliche Antwort
Ihr NSError muss als Optional
definiert werden, da er null sein kann:
var error: NSError?
Sie möchten auch berücksichtigen, dass beim Analysieren ein Fehler aufgetreten ist, der nil
zurückgibt, oder dass beim Analysieren ein Array zurückgegeben wird. Dazu können wir ein optionales Casting mit dem as?
Operator.
So bleibt uns der vollständige Code:
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
}
Wie bereits erwähnt, muss der Fehler als optional definiert werden ( https://developer.Apple.com/library/prerelease/ios/documentation/Swift/conceptual/buildingcocoaapps/AdoptingCocoaDesignPatterns.html )
Jedoch - Dieser Code WIRD ABSTURZEN, wenn ein Fehler auftritt und nil zurückgegeben wird. Das "Als NSDictionary" wäre der Schuldige:
var data: NSDictionary =
NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: &error) as NSDictionary;
Sie müssen das JSON-Parsing folgendermaßen durchführen:
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)")
}
Das wird funktionieren. Denken Sie auch daran, dass json als Array zurückkehren kann. Anstatt nil für die Optionen zu übergeben, können Sie alles übergeben, was Sie möchten, zum Beispiel:
NSJSONReadingOptions.AllowFragments
wenn du möchtest.
Jetzt in der Beta-3
var error: AutoreleasingUnsafePointer<NSError?> = nil
var data: NSDictionary = NSJSONSerialization.JSONObjectWithData( w, options:.AllowFragments, error: error ) as NSDictionary;
Überprüfen Sie mit dem folgenden Code:
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)")
Versuchen Sie es mit
var error: AutoreleasingUnsafePointer<NSErrorPointer?>=nil