Accessing a Property List (.plist) From SwiftUI

Daily Coding Tip 023

The settings for your iOS or Mac app exist in a file called info.plist, which you have probably had to modify at some point. It is made up of a dictionary with string keys for every value. If you hover over the bottom row, you’ll see ⊕ and ⊖ buttons appear. Pressing ⊕ creates a new row, giving you autocomplete suggestions of everything Apple could possibly want you to include, such as your explanation for why you want access to the camera or GPS location.

Your info.plist is not added to your app bundle, so you will not be able to access it at runtime.

Create a new SwiftUI iOS or Mac project, right-click the yellow folder in the left panel, and choose New file. Scroll down to the Resources section, and choose Property List. I’m just going to use the default filename Property List.plist, but you can call it whatever you want. Create a new row in the Property List, call it Person 0, and change the type from String to Dictionary. This allows you to create a dictionary for every object in your plist, which is more interesting than just a list of properties.

Hover over Person 0 and press ⊕ to create rows inside the dictionary. I created a String for name and wrote my name as the value. I created a Date for birthday, and edited the default current date to be the day I was born in that format. Finally I added a Boolean value called Loves Swift, and set it to YES! Now that we have this structure, it’s conceivable that we could create many people, each their own dictionary, with the same data inside.

I’m going to stick with one person though.

Now that we have a structure for our data, let’s see if we can open it and display it in SwiftUI!

There’s a lot going on here. The getPlist function is based on one created by LearnAppMaking. In this case it returns an NSDictionary, instead of an array of strings. This will make it easier to obtain data from it later. You’ll notice that the function is static, along with the plist and person0 properties below it. Making this data static makes it exist at the level of the ContentView structure itself, rather than being different for every individual instance of it.

This allows me to do things like initialize the static properties using the getPlist function or other properties, which would not be possible if they were instance properties or functions. I am creating the name, birthday and lovesSwift properties, but as you can see it’s okay to use instance properties for those. As you can see from all of the question marks, every property is an optional with the possibility of being nil if our property list doesn’t load properly.

I use a computed property to create a DateFormatter, which will be used to show the date on the screen.

The SwiftUI is a simple VStack that shows the data. You may notice the ?? nil-coalescing operator, which is used as a say to give a default value in the case that the value is nil. If there is no name, “No name” will be displayed. If there is no birthday, the current date and time will be displayed. Finally, the default value for Loves Swift is false, because if you didn’t bother to give it a value, you must not love it enough.


Get more Daily Coding Tips in your inbox!