Realm-Swift Type Projections
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.
- iOS 15+
- Xcode 13.2+
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.

It's currently iOS-only, but it would also sync with any Android drawing app that is connected to the same Realm back end.
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.
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 theCGFloat
used by the model - A method to convert a
CGFloat
into aDouble
:
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.