Strongly typed access to Info.plist file using Swift

How Codable can help us to parse and access to values saved in .plist by keeping them type-safe

Daniele Margutti
Apr 30, 2018 · 2 min read

As Apple’s developers we started facing the type-safe constraint in our code with the advent of Swift.

From my side I’ve always tried to fully embrace this approach even if often this means to come to a deal with several parts of the UIKit which are obviously created with a different — more dynamic — paradigm in mind.
Sometimes is easy, some other less, but they still a good excercise to think outside the box to keep our code safer and cleaner.

Recently I’ve re-faced an apparent trivial task; I would to read the configuration of an application saved inside the Info.plist file of the app.

My Info.plist contains an additional node called configuration with several data inside: server_url, environment_name an a bunch of other keys.

Values are dynamic and assigned based upon the current schema you have set to launch the application (ie. $(SERVER_URL) is the url server which has a value or another depending from the configuration environment like TESTING/PRODUCTION).

The goal is to get these values by keeping the type of the data.
The most straightforward approach is to use the magic behind the Codable protocol (this article is not about Codable, you can found tons of articles around like here, here or here).

The code is pretty simple, you need just specify a Codable structure which can handle your interesting data, then allocate the Plist class with the generic type.

In our example data we can provide the following structure:


You can use the class itself to read every other file other than Info.plist; just pass a .plist("otherPListFile") as init parameter and provide your own Codable structure.

iOS App Development

Stories and technical tips about building apps for iOS, Apple Watch, and iPad/iPhone

Daniele Margutti

Written by

Senior Developer — Mobile Tech Lead @—

iOS App Development

Stories and technical tips about building apps for iOS, Apple Watch, and iPad/iPhone

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade