Hi all. I've been looking at examples of using alamofire, json etc.
For a downloader I have a function that takes some parameters of String:Any
Then my view controller calls this function passing in parameters, uses almofire to retrieve the data, and returns "json"
Then in my view controller I have after that
guard let responseFromJson = json as? [String: Any]
else {
print("Whatever message")
return
}
This always goes to the else statement and responseFromJson is of value nil. I'm learning Swift and I'm a bit confused, because every other example I've seen online uses if not the same method, very similar and every question has answers saying the same.
My debugger says json is of type "Any?" with NSDictionaries in it, but the terminal tells me it's of NSArray with NSDictionaries. If I do
guard let responseFromJson = json as? NSArray
It's the only way I don't get a nil.
I've been at this for hours and I'm very lost.
Thanks
EDIT: Folks let this be a lesson on why not to program 12 hours straight. A few minutes after posting this and getting off, I realized "wait, I have an array of dictionaries, not a single dictionary". The solution is
guard let responseFromJson = json as? [[String: Any]]
Which is another way of saying Array<DictionaryString:Any>
Sorry everyone!
Congrats to have found it. A good night over a problem is most often very effective…
Another lesson here.
When you post code, you have to give context information.
Here, you should have shown how json is defined (what's its type…), and even give an example of JSON you get to build json.
Good continuation. Don't forget to close the thread.