The compiler error is
No precise matches in name to class technique 'jsonObject'
So it means, that JSONSerialization
, has a static technique that look alike, however it’s not the identical.
So whenever you encounter this difficulty, it is normally a typo or dangerous kind.
In your case, it is dangerous sorts.
Let’s begin by examine the kind of response
.
You would possibly must remark the traces that causes the error first.
Mouse over the response
, preserve Choice key tapped (the cursors transforms right into a query mark), after which click on on response
.
You may get its kind: AFDataResponse<Information?>
.
Now, let’s have a look at JSONSerialization
doc, and it has these two strategies (I strongly guess you need the primary one):
class func jsonObject(with: Information, choices: JSONSerialization.ReadingOptions) -> Any
class func jsonObject(with: InputStream, choices: JSONSerialization.ReadingOptions) -> Any
See the difficulty?
Information != AFDataResponse<Information?>
To entry the Information
object from response
, it is with response.knowledge
:
let json = strive JSONSerialization.jsonObject(with: response.knowledge!, choices:.allowFragments) as! Dictionary<String, Dictionary<String, Any>>
Unrelated, however since it is a SO query, we are likely to usually go strictly to the difficulty, however I observed a number of issues although, and I would not need somebody to duplicate them when studying.
Now, you’re utilizing drive unwrap (use of !
), if there is a matter: response.knowledge
being nil, json
not being a Dictionary<String, Dictionary<String, Any>>
, it can crash, simply in your info.
catch let error as NSError
could be changed with simply catch
choices:.allowFragments
just isn’t wanted. To be wanted, the JSON must be only a Bool
, a String
, or a Quantity
. In different phrases, at prime degree it is not an Array
nor a Dictionary
.
You may also wish to examine the success or not of the response, else the entire response may be completely different, after which, it may not be a Dictionary<String, Dictionary<String, Any>>
and can crash. We would think about responding a 500 error with a HTML error code, and also you as!
will make it crash.
So, I would write it that method:
AF.request(webservice, headers: headers).authenticate(with: credential).response { response in
print(response)
change response.end result {
case .success(let knowledge):
do {
guard let knowledge = knowledge else { print("Success, however empty response"); return }
let json = strive JSONSerialization.jsonObject(with: knowledge)
guard let jsonDict = json as? [String: [String: Any]] else {
print("JSON just isn't a [String: [String: Any]]: (json)")
return
}
//Use jsonDict
} catch {
print("Error whereas decoding: (error)")
}
case .failure(let error):
print("Error: (error)")
}
}