Designing An API Service Manager and API Data Model in Swift for Weather Application

Yasir
2 min readMay 5, 2021

Let us handle the API and API calls in any application 🥸

Selecting API

Openweathermap is one of the free api provider for weather information , We will be using their api in this application . You can go through this link to understand the documentation about the APIs .

We will just pass the city name with URL and APIKey to get the weather information .

http://api.openweathermap.org/data/2.5/weather?q={cityName}&appid={YOUR APIKEY}

API Data Model

Before making an api call in our application , let us create a model which stores the information getting from API while decoding it .

Since we need only few informations from weather , we are creating only those variables in our model . Depending upon the information you need in your application , you can add more details in this model .

Also make sure to have the same json model from the api . The model you have created and the model you are getting from API should match .

API Service Manager

Now let us perform some api call . We can also use alamofire to make network calls , but we are using all default tools provided by apple . Also to parse the data we can use SwiftyJSON library , but we are not using that here .

Now you can create your own UI and just use this manager to your application. Select an api of your chooise and create the model based on your need and based on the information you are getting by the API .

Where to Go From Here ?

Me on Socials : LinkedInGitHub

Check this link : https://bit.ly/38HOQeb
Clap if you like this article !

Thank YOU !

--

--

Yasir

Exploring the Swift, SwiftUI, and Apple universe.