EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right

Realm-Swift Type Projections

Andrew Morgan4 min read • Published Jan 26, 2022 • Updated May 09, 2022
Facebook Icontwitter iconlinkedin icon
Rate this tutorial


Realm natively provides a broad set of data types, including Bool, Int, Float, Double, String, Date, ObjectID, List, Mutable Set, enum, Map, …
But, there are other data types that many of your iOS apps are likely to use. As an example, if you're using Core Graphics, then it's hard to get away without using types such as CGFloat, CGPoint, etc. When working with SwiftUI, you use the Color struct when working with colors.
A typical design pattern is to persist data using types natively supported by Realm, and then use a richer set of types in your app. When reading data, you add extra boilerplate code to convert them to your app's types. When persisting data, you add more boilerplate code to convert your data back into types supported by Realm.
That works fine and gives you complete control over the type conversions. The downside is that you can end up with dozens of places in your code where you need to make the conversion.
Type projections still give you total control over how to map a CGPoint into something that can be persisted in Realm. But, you write the conversion code just once and then forget about it. The Realm-Swift SDK will then ensure that types are converted back and forth as required in the rest of your app.
The Realm-Swift SDK enables this by adding two new protocols that you can use to extend any Swift type. You opt whether to implement CustomPersistable or the version that's allowed to fail (FailableCustomPersistable):
In this post, I'll show how the Realm-Drawing app uses type projections to interface between Realm and Core Graphics.


The Realm-Drawing App

Realm-Drawing is a simple, collaborative drawing app. If two people log into the app using the same username, they can work on a drawing together. All strokes making up the drawing are persisted to Realm and then synced to all other instances of the app where the same username is logged in.
Animated screen captures showing two iOS devices working on the same drawing
It's currently iOS-only, but it would also sync with any Android drawing app that is connected to the same Realm back end.

Using Type Projections in the App

The Realm-Drawing iOS app uses three types that aren't natively supported by Realm:
  • CGFloat
  • CGPoint
  • Color (SwiftUI)
In this section, you'll see how simple it is to use type projections to convert them into types that can be persisted to Realm and synced.

Realm Schema (The Model)

An individual drawing is represented by a single Drawing object:
A Drawing contains a List of Line objects:
It's the Line class that uses the non-Realm-native types.
Let's see how each type is handled.


I extend CGFloat to conform to Realm-Swift's CustomPersistable protocol. All I needed to provide was:
  • An initializer to convert what's persisted in Realm (a Double) into the CGFloat used by the model
  • A method to convert a CGFloat into a Double:
The view can then use lineWidth from the model object without worrying about how it's converted by the Realm SDK:


CGPoint is a little trickier, as it can't just be cast into a Realm-native type. CGPoint contains the x and y coordinates for a point, and so, I create a Realm-friendly class (PersistablePoint) that stores just that—x and y values as Doubles:
I implement the CustomPersistable protocol for CGPoint by mapping between a CGPoint and the x and y coordinates within a PersistablePoint:


Color is made up of the three RGB components plus the opacity. I use the PersistableColor class to persist a representation of Color:
The extension to implement CustomPersistable for Color provides methods to initialize Color from a PersistableColor, and to generate a PersistableColor from itself:
The view can then use selectedColor from the model object without worrying about how it's converted by the Realm SDK:


Type projections provide a simple, elegant way to convert any type to types that can be persisted and synced by Realm.
It's your responsibility to define how the mapping is implemented. After that, the Realm SDK takes care of everything else.
Please provide feedback and ask any questions in the Realm Community Forum.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial

How to Do Full-Text Search in a Mobile App with MongoDB Realm

Jul 14, 2023 | 3 min read

The MongoDB Realm Hackathon Experience

Sep 05, 2023 | 7 min read

Making SwiftUI Previews Work For You

Mar 06, 2023 | 13 min read

Migrating Android Apps from Realm Java SDK to Kotlin SDK

Apr 02, 2024 | 10 min read
Table of Contents
  • Introduction