iOS
...
Parse Swift SDK
Data Objects

Data Types

14min

Parse data types on Swift

Introduction

When saving data on a Back4App Database, each entity is stored in a key-value pair format. The data type for the value field goes from the fundamental ones (such as String, Int, Double, Float, and Bool) to more complex structures. The main requirement for storing data on a Back4App Database is that the entity has to conform the ParseSwift protocol. On its turn, this protocol provides a set of methods to store, update and delete any instance of an entity.

In this guide, you will learn how to create and setup an entity to save it on your Back4App Database. In the project example the entity we are storing encloses information about a Recipe.

This tutorial uses a basic app created in Xcode 12 and iOS 14.

At any time, you can access the complete Project via our GitHub repositories.

Goal

To understand how objects are parsed and stored on a Back4App Database.

Prerequisites

To complete this quickstart, you need:

Understanding our Recipes App

The app functionality is based on a form where one can enter information about a recipe. Depending on the information, the data type may vary. In our example the recipe has the following features:

Field

Data Type

Description

Name

String

Name of the recipe

Servings

Int

Number of servings

Available

Bool

Determines whether the recipe is available or not

Category

Category

A custom enumeration which classifies a recipe in three categories: Breakfast, Lunch and Dinner

Ingredients

[Ingredients]

The set of ingredients enclosed in a customIngredient struct

Side options

[String]

Names of the additional options the recipe comes with

Nutritional information

[String:String]

A dictionary containing information about the recipe’s nutritional content

Release date

Date

A date showing when the recipe was available

Additionally, there are more data types which are used to implement Database functionality like relation between objects. These data types are not covered in this tutorial.

Quick reference of commands we are going to use

Given an object, say Recipe, if you want to save it on a Back4App Database, you have to first make this object to conform the ParseSwift protocol (available via the ParseSwift SDK).

Swift


Before storing instances of this object in a Back4App Database, all its properties must conform the Codable and Hashable protocols.

We make use of the following methods for managing these objects on the Back4App Database:

Create
Update
Read
Delete


1 - Create the Recipe App Template

We start by creating a new XCode project. This this tutorial the project should look like this

Document image


At any time, you can access the complete Project via our GitHub repositories.

Go to Xcode, and find the SceneDelegate.swift file. In order to add a navigation bar on top of the app, we setup a UINavigationController as the root view controller in the following way

Swift


The root view controller class (RecipesController) for the navigation controller is a subclass of UIViewController in which we will layout a form to create and update Recipe objects on the Back4App Database.

2 - Setup the Recipe object

Objects you want to save on your Back4App Database have to conform the ParseObject protocol. On our Recipes app this object is Recipe. Therefore, you first need to create this object. Create a new file Recipe.swift and add the following

Swift


where we already added all the necessary properties to Recipe according to the recipes’s features table.

The Ingredient data type is a struct holding the quantity and the description of the ingredient. As mentioned before, this data type should conform the Codable and Hashable protocols to be part of Recipe’s properties

Swift


Additionally, the property category in Recipe has an enumeration (Category) as data type which also conforms the corresponding protocols

Swift


3 - Setting up RecipesController

In RecipesController we should implement all the necessary configuration for the navigationBar and the form used to capture all the Recipe properties. This tutorial does not cover how to implement the layout for the form. We then focus on the logic related with managing data types using ParseSwift SDK. Below we highlight the key points in RecipesController which allow us to understand how we implement the connection between the user interface and the data coming from your Back4App Database

Swift


3 - Handling user input and parsing a Recipe object

In a separate file (called RecipesController+ParseSwiftLogic.swift), using an extension we now implement the methods handleSaveRecipe(), handleUpdateRecipe() and handleUpdateRecipe() to handle the input data

Swift


4 - Run the app!

Before pressing the run button on XCode, do not forget to configure your Back4App application in the AppDelegate class!

The first time you run the project you should see something like this in the simulator (with all the fields empty)

Document image


Now you can start entering a recipe to then save it on your Back4App Database. Once you have saved one recipe, go to your Back4App dashboard and go to your application, in the Database section you will find the class Recipe where all recipes created by the iOS App.

In Particular, it is worth noting how non-fundamental data types like Ingredient, Recipe.Category or dictionaries are stored. If you navigate through the data saved under the Recipe class, you will find that

  • The nutritionalInformation dictionary is stored as a JSON object.
  • The [Ingredients] array is stored as an array of JSON objects.
  • The enumeration Recipe.Category, since it is has an integer data type as RawValue, it is transformed to a Number value type.
  • The releaseDate property, a Date type value in Swift, is also stored as a Date type value.

To conclude, when retrieving data from your Back4App Database, you do not need to decode all these fields manually, ParseSwift SDK does decode them automatically. That means, when creating a query (Query<Recipe> in case) to retrieve data, the query.find() method will parse all the data types and JSON objects to return a Recipe array, there is no additional parsing procedure to implement.