Implementing Protocols in Swift

Joe Vargas
2 min readFeb 28, 2021

My achilles heel so far with learning Swift is the use and implementation of protocols. My experience with protocols has always been I get what they do and how helpful they are BUT they are challenging for me to implement.

My earliest use of protocols was when I made a simple weather app to spruce up my portfolio. The app consisted of two view controllers. The secondary view controller consisted of a search field where a user can input their city. When they tapped on the search button down below, an implemented protocol function passed the city name over to the main view controller, placed the city name of type string in the api query function and returned said city’s weather data. After a while, it was quite simple to understand how a protocol is used to pass info between view controllers.

Recently, I was challenged with creating a storefront app made for selling shoes. It consisted of the main view having a tableview displaying the shoes. I created a custom tableview cell that displayed the name of the shoe, an image of the shoe and price. Additionally, the cell had two buttons: one of a heart to represent when the user likes or dislikes the shoe and button that adds the shoe to the store.

In a nutshell, I needed the tableview to be in constant communication with the view controller because the table view needed to inform the view controller which of its cells had their buttons tapped. Additionally, it needed access to the shoe array that is the local database.

Learning how to implement a protocol to help with this was going to be a bigger challenge than my weather app.

Below is a brief video of me explaining how I implemented a protocol to pass data and an action.

Happy coding!

--

--

Joe Vargas

iOS Developer at MartianCraft 📍Houston, TX 🚀🌎💫