I'm going through the Argo documentation for Swift JSON Parsing (https://github.com/thoughtbot/Argo) and they provide a simple code snippet that should retrieve the JSON data, but I get an error when running it. The snippet is:
// Wherever you receive JSON data:
let json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil)
if let j: AnyObject = json {
let user: User? = decode(j)
}
and the error I get is "Statements are not allowed at the top level", on the line that says if let j...
Why would this cause an error and why is this in their github readme if it doesn't work?
Because it is a snippet. You are expected to use it inside a function, where it is legal. Executable code can appear only in a function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With