Using Text Files with your apps

One of the challenges that a developer faces is to use a database or a persistent storage. At the start of mobile development, the most common solutions were sqllite, and plist files. Fast forward from 2009 to 2021, apple has been releasing quite a few libraries and one of them that we will look at today allows us to work with text data. It is namely to work with CSV files or JSON files but because it can do a lot more than simply import or read a CSV file, we shall look at this method and how first we can read the data, then in the next article we will look at how to use the same as a persistent storage or a text database.The library we shall use is TabularData and the way to integrate it into a project is simply import TablularData

If you want to know more about this Library, you can use the Apple Documentation that is found at which is as useful to someone (trying to use this library) as horns on a donkey. i.e. the documentation is completely useless and serves no purpose. So let us go step by step and try to see how we can use this library.

Step 1. – Open a CSV file

Let us first create and save this data into a csv file and we will use this in our code. We can save it as cats.txt

Steve,3,Alexander Shvets,Bengal,/cats/bengal.jpg,Brown,Stripes,Short,Medium
Siri,2,Alexander Shvets,Domestic short-haired,/cats/domestic-sh.jpg,Black,Solid,Medium,Medium
Fluffy,5,John Smith,Maine Coon,/cats/Maine-Coon.jpg,Gray,Stripes,Long,Large

Now from the code, we need to first load this csv file using the TabularData library, the function that allows us to open this file is called DataFrame. It takes an url to load the data, in our case since we are using a local file, we can provide it the path of our file.

when we run this code, we can see that it prints the following

We can see that the library has identified the headers and their types, it also displays how many rows are present and how many columns are present in the file. If the file could not be loaded, it would have thrown an error. The most common error that one can find with reading csv files is when the separator is used in the data.

NOTE: The full form of CSV is Comma Separated Values, and therefore the default separator is a comma. A comma can also be used in the data which in turn can cause issues as it splits the text into an additional column, and a mismatch of columns creaks the import.

Now we can work with the data slices either by row or by column.

Column Selection

and this would print the data for just that column color as can be seen in the image below

Row Selection

Which would display the data for the row as in the image below

So we can read a csv file, get the data row by row. However, if you wanted to get data via columns, it is a bit tricky. One cannot simply iterate through the columns in the row and get the column required. The reason for that is the pivot is first by columns and then by rows. So if we wanted to get the texture for this cat, which is Solid, we would have to first get the column by name, and then retrieve the row by index (in this case we know it is 1).

Searching and limiting data

If we did not want to retrieve data statically but instead search and filter for data, we can also do that buy using all of the standard functionality that swift has to offer.

and this now shows us the two records that are found that match cats with striped texture

In this code above we added a new keyword called ColumnID, these are used to identify the column headers. It is useful to help identify the column and the content inside it. With swift being cutting edge and this library being new, it has these unsightly implementations where you have to use String.self and Int.self etc, hopefully these will be abstracted in the future releases.

Similarly, when we print, the information can get a bit too long, we can control the output by using the formatting options on the description function of the Data.

and this will now shrink all of the columns to a width of 12 and display a maximum of 2 rows

Similarly we can also determine which rows we want to select when we load the data or query the data by specifying the column names and types. Though this seems to be the long way at the moment, there would be a better and easier way in the future. Till then the code to limit the columns would look a bit like this

The same can also be done from scratch where the structure for a csv file is created in code and the resulting data then written to the file. In the next article we shall look at how we can create the csv file from code.

If videos are more appealing than reading an article, then Apple has a video that talks about some of these things in their video found here . However, there are a lot of things that are still missing from both the Apple Documentation and this video when you start to implement this in your own code. So hopefully this helps with that.

Views All Time
Views All Time
Views Today
Views Today
Posted in Article, Basics, Code, Tutorial, Uncategorized and tagged , .