Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Error: Statements are not allowed at the top level [duplicate]

Tags:

json

swift

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?

like image 686
mattgabor Avatar asked Jun 16 '15 23:06

mattgabor


1 Answers

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.

like image 58
matt Avatar answered Sep 21 '22 19:09

matt