Next, we need to create a file to represent our data. Our Explore list displays an image and a name that match the corresponding image and name that we see in our Explore.plist file. Let's create this ExploreItem file now:
- Right-click on the Model folder and select New File.
- Inside of the template screen, select iOS at the top and then Swift File. Then, hit Next.
- Name the file ExploreItem, and then hit Create.
The only thing in this file is an import statement.
The import statement allows us to import other libraries into our file, giving us the ability to see inside of these libraries and use properties from them. Foundation is one of Apple's core frameworks, and it has a bunch of tools that we can use while we program.
Since we do not need to use inheritance, we are going to make this file a struct. Add the following to your file:
struct ExploreItem {
}
Now that we have declared it a struct, let's add the two properties we need for this file—an image, and a name. For both of these properties, we are going to make them String data types. For the title, this makes sense, because it is text that we are displaying in our Collection View. However, for the image, using a String data type might not seem as obvious. The reason we are making the image a data type of String is because, to get it, we have to access it by name. For example, american.png is the filename for the American cuisine image. Add the following to the inside of your curly braces ({ }):
var name:String?
var image:String?
We have now added two properties, one for the image and one for the name, both of which are optional. Since we cannot give either of them an initial value, we have to make them optional.
Your file should look like the following:
struct ExploreItem {
var name:String?
var image:String?
}
We next need to add one more thing to this file.
We take the dictionary data we get from the plist and create an ExploreItem for each item. Our dictionary now looks like the following:
["name": "All", "image": "all.png"]
We need to pass this dictionary object to our ExploreItem. When you are passing a dictionary object, you are required to create a custom initializer. Our initializer takes a dictionary object into it. Then, we can set each item from the dictionary to the data of both of our properties, image, and name.
When you create a struct, by default, you get an init() method that has all the properties you created in the parameters.
For example, our ExploreItem will have a default initializer that looks like the following:
init(name:String, image:String)
Instead of using this initializer, we will create our own to pass a dictionary object into it.
To create a custom initializer, we are going to use what is called an extension, which gives us the ability to extend our code and add more functionality to it. Inside of your ExploreItem file, after the ending curly brace, add the following:
extension ExploreItem {
}
Next, let's create our custom initializer that takes a dictionary object into the parameters. Add the following between the curly braces of the extension we just added:
init(dict:[String:Any Object]) {
}
We have now created an init() method in the parameters, which accepts a dictionary object. As stated in the preceding section, we know that our data looks like the following:
["name": "All", "image": "all.png"]
To pass each value, we need to use the dictionary syntax, such as:
dict["name"]
dict["image"]
Let's proceed by mapping the dictionary data to our two properties. Add the following inside of the init() method curly braces:
self.name = dict["name"] as? String
self.image = dict["image"] as? String
}
Since our dictionary value is AnyObject, we have to specify that our data is a String by using the as? String at the end.
We now have our data item set up for our Explore view (cuisine list), and your file should look like the following:
extension ExploreItem {
init(dict:[String:AnyObject]) {
self.name = dict["name"] as? String
self.image = dict["image"] as? String
}
}
Let's now focus on our data manager. We want our data manager to handle parsing the plist and giving us the data. Since our data will be coming from a plist, we need to have a method that will get the data from the plist first.