In the next chapter, we will deal with data, but for now, we can mock up some data to set up our structure. We will use a plist to load our data, just like we did in the last chapter.
Let's create the MapDataManager file now:
- Right-click on the Map folder and select New File.
- In the Choose a template for your new file screen, select iOS at the top and then Swift File. Then hit Next.
- Name this file MapDataManager and then hit Create.
- Next, we need to define our class definition, so add the following under the import statement:
class MapDataManager {}
- Inside of the class declaration, add the following variables:
fileprivate var items:[RestaurantItem] = []
var annotations:[RestaurantItem] {
return items
}
Note that we are keeping our array private since there is no reason to have to access this outside of the class.
- Now, let's add the following methods inside of our class declaration, after our variables:
func fetch(completion:(_ annotations:[RestaurantItem]) -> ()) {
if items.count > 0 { items.removeAll() }
for data in loadData() {
items.append(RestaurantItem(dict: data))
}
completion(items)
}
fileprivate func loadData() -> [[String:AnyObject]] {
guard let path = Bundle.main.path(forResource: "MapLocations", ofType: "plist"),
let items = NSArray(contentsOfFile: path) else { return [[:]] }
return items as! [[String : AnyObject]]
}
Your file should now look as follows:

- The fetch() and loadData() methods are the same as those that we had in the ExploreDataManager file. However, the fetch() method here has something new inside of its parameters, specifically:
completion:(_ annotations:[RestaurantItem]) -> ())
This is called a closure block, which allows us to signify when we have completed the method, and it then dictates an action to occur (here, returning an array of annotations). We will use these annotations to load pins on our map. We are looping through the for...in loop; when we are done, we call completion(). When we get to our MapViewController , you will see how we write this.
Now, let's take a look at our MapLocations.plist file:

This file is the same structure as our ExploreData.plist file. Our Root is an array, and each item inside of our Root is a dictionary item. There is an acronym that many programmers call DRY (don't repeat yourself). Since both plist files have an array of dictionary objects, we can update our code so that we can use the same method in multiple places.