The property list file is known as the plist file that contains properties related to a specific application in a structured data format that can be used later.
July 20, 2022
You have developed many iOS applications in which you need to store some data while coding. A new function has been introduced for iOS developers because the previous function is complex to use and work with.
The Info plist (or property list) file is a structured text file that contains essential configuration information for a bundled executable. This new file configures the mobile application and describes how it runs. Now learn how to add this property list in an iOS app.
In plist, we can manage this file programmatically and directly access the file too.
Here’s an example:
In this image, the .plist file name is EmployeeData.plist and the root of the .plist file is a dictionary type and the arrEmployee is key to the root dictionary. The value type of key arrEmployee is an array.
How Can Create .Plist File?
Now let’s get to know how to create plist file programmatically and how to store in the document directory.
And, after creating a .plist file first we have created nil plist file structure.
Here is the code :
Here is the getpath() func who return a path of document directory for where plist is stored.
After getting path with use of this function first make a nil dictionary with required structure.
So in the above code, first I have created a root dictionary with the name of dictContent, then set prototype as [String:[Any]], key is String and value is array of any.
Then i’ve created an array with the name of arrEmployee, and set prototype [ [String: String]], array of [String: String] type dictionary.
And then set arrEmployee with key “arrEmployee” of dictContent (Root Dictionary).
Then convert dictionary to NSDictionary and write plistContent to file path, (getting path using getPath ( ) function).
So this is how to create a .plist file to document directory.
Now let's get to know how to write data in this file.
How To Write Data To .Plist File:-
Here is a demo for getting write and read data from the .plist file.
I've created a simple form presentation from filling employees' general data.
Here is all outlets of UI controllers which are used in the program:
// variables : -
var pathToDatabase = String()
var fileUrl : NSURL?
On the submit click I’m writing data to plist file and here is the code:
So in this code, first I'm retrieving the root dictionary from plist using getPath function like above.
After that, retrieving an array (with name arrEmployee ) from root dictionary (with name dictionary), for that gives key String to dictionary it will give the value from Dictionary.
Then, I’m making and dictionary (with name of ( dictEmployee ) for containing all my necessary value data from image view and text fields, which I want to store in plist file.
And then appending this dictionary to arrEmployee.
Again set this modified arrEmployee in the root dictionary ( dictionary ) with the same key string( “arrEmployee” ).
Then same as above converting a dictionary to NSDictionary and write a dictionary to file path (using getPath( ) function).
Let success : Bool = plistContent.write(toFile: self.getpath(), atomically : true).
This method return a bool value based on writing dictionary has been successful or not.
If the result is true then successfully data written, else not.
This is how we can write data to the plist file.
How To Read Data From .Plist File?
Now let's get to know how to retrieve data and how to use it.
For showing data I’ve used table view, and here is the code and outlets,
Create a UITabelviewcell file with this outlets,
In this function, I've just retrieve the root dictionary(main dictionary).
Then retrieve arrEmployee from mainDictionary with key string “arrEmployee”.
Then use arrEmployee Array in tableview.
Get path function:
All tableview methods,
In the cellForRowAt method retrieved a dictionary from arrEmployee with the name of data dictionary.
And then assign data dictionary value to the cell’s labels and image view.
For Show .plist file in document directory, just print your plist file path using getpath ( ) function in the Xcode log output area.
Then select path and right-click on it, select options show in finder and you can show list file with the name you have given it.
This is how .plist file looks with data.
How To Delete Data From .Plist?
For deleting data from plist file, I‘ve applied code in tableview’s commit editingStyle method.
On right swipe of table view method,
In the above function, the same as before first i’ve to retrieve the root dictionary with the use of getPath ( ) function and then retrieve the array from the dictionary with the name of are employee.
Then with the use of a swiped cell index path, I’ve removed the object from arrEmployee at the index path .row.
Then again set arrEmployee to root dictionary, convert it to NSDictionary and write data to file path using getpath( ).
So, this is how easiest we can delete data from plist.
And after delete if no record is available then, NoDataAvailable label is show.
How To Delete Data From .Plist?
For updating data same as before we need to get data from .plist and show to edit view controller and for that I’ve coded it to table views did select method. Add this variable to submit a data view controller from where you have inserted data first.
Then in didselect method of tableview of show data view controller :
It will look like, after clicking on table views particular cell.
Then applied all changes whatever you want and then on submit click again condition for the update.
Applying changes in data, changing profile, surname.
Call this function in the submit button clicked.
So, this is how easily we can update plist data.
In this blog, you have learned to create a property list (or info plist). It is usually used by developers during iOS application development. It helps to store essential information in a structured data format in the form of a dictionary with key-value pairs.