Exploring the API Manager file

We just created our API Manager folder. Now, let's create the API Manager file:

  1. Right-click on the Misc folder and select New File.
  2. On the Choose a template for your new file screen, select iOS at the top. Then, select Swift File. Then, hit Next.
  3. Name this file RestaurantAPIManager, and hit Create.

We need to define our class definition first; therefore, add the following to the import statement:

struct RestaurantAPIManager {
static func loadJSON(file name:String) -> [[String:AnyObject]] {

The next bullet list explains what we need to write when we want to call the loadJSON method inside the RestaurantAPIManager file.

var items = [[String: AnyObject]]()
guard let path = Bundle.main.path(forResource: name, ofType: "json"), let data = NSData(contentsOfFile: path) else {
return [[:]]
}

Here, we are using a do...catch. As a reminder, a do-catch statement is used to handle errors by running a block of code. To employ it, we must utilize it with what is known as a try. First, we need to try and serialize or convert the data from the JSON file; if we are successful, we can then access the information inside that file. To obtain the restaurant items in the JSON file (all of which are located inside the restaurant's node), we used json["restaurants"].

Next, we cast this using as? as an array of dictionary objects. Also, since our data types are mixed, we used AnyObject to accept the dictionary of mixed data types. Finally, we set our data to the array of items. We now have the same structure, and the array of dictionary objects that we had in the Map section:

do {
let json = try JSONSerialization.jsonObject(with: data as Data, options: .allowFragments) as AnyObject
if let restaurants = json as? [[String: AnyObject]] {
items = restaurants as [[String : AnyObject]]
}
}
catch {
print("error serializing JSON: (error)")
items = [[:]]
}
return items

This entire class is built so that we can pass any name we want; it will return data if it finds the file.