How to read and write to CSV files using Xuni FlexGrid for iOS

Several weeks ago, we explored reading and writing CSV files using Xuni for Android. This process has some general similarities on iOS and a few differences to working with CSV files on Android. In this article, we’ll examine loading data from and saving data to a local CSV file on the iOS platform using Xuni FlexGrid. This guide uses the same stock data CSV that we used for our Android example. We’ll deal with loading the data from a CSV saved in the mainBundle, optionally loading from the documents directory, and saving the edited FlexGrid to the documents directory.

Reading a CSV File

The first step to implementing our example is to create a class that handles reading a CSV file and parsing the data (much like we did on Android). The implementation is a little different though as our file contents will be read in as a long string. We'll still need to split the data into individual lines, and also split the values separated by a comma. We'll then iterate through the data and add it directly to our model (which we'll cover in the next section):

Creating a Model for your Data

The model allows us to strongly type our data, and provides some form to what would otherwise be a large group of strings that we've read from our CSV. Since we're working on iOS we'll be using many descendants of NSObject. A single line of the stock.csv file looks like this: GOOG,01-Sep-2015,602.359985,612.859985,594.099976,597.789978,3680200 These values reflect a ticker, date, open, high, low, close, and volume, which means we’ll need to create properties that correspond to each. Likewise, we’ll provide a mechanism for taking the the FlexGrid's itemsSource (which is an NSMutableArray on iOS) after it’s been edited and converting it back into a string for the purpose of saving the data to disk. Our header file declares the the properties we'll need:

Saving to a CSV File

Saving the data from our grid back to CSV is essentially a reversal of the earlier process of reading the CSV. First, we'll need to provide a location to write the file. For this example, we’re going to save the file to the documents directory. Unlike Android, there's no notion of external storage since iOS devices are much more uniform in design and available space, and we also don't have to concern ourselves with permissions in the manifest. The documents directory isn't something that a user will access directly on their device, though it is something that we can access on our simulator via the Finder on a Mac or in code in our application. The file path should be something along the lines of: Users\"UserName"\Library\Developer\CoreSimulator\Devices\ "DeviceID"\data\Containers\Data\Application\"ApplicationID"\Documents The implementation file is fairly straightforward, and it requires using the correct paths to write a string to the documents directory. If some type of error is encountered we'll return it so that we can notify the user elsewhere.

Setting up the ViewController

The final step is to configure the ViewController so that it loads data into our FlexGrid when the application starts. We'll also provide a button that allows us to save the modified grid back to CSV. I’m including a “built-in” stock.csv file which is included as part of the mainBundle and is present in our XCode project: Much like on Android, this application will check if a modified stock.csv is present in the Documents folder. If it exists then that data will be loaded into the FlexGrid. If the file hasn't been created yet we will instead load the stock.csv included with the application in the mainBundle. This logic will all be added into our viewDidLoad method:

Wrapping Things Up

Xuni FlexGrid can easily provide a similar experience across multiple platforms which a little help from the API's available on iOS and Android (or alternatively via Xamarin). With a small amount of effort, you can provide a very clean and easy mechanism for viewing and editing your data on either platform.