Classes

The following classes are available globally.

  • A 128-bit IEEE 754-2008 decimal floating point number.

    This type is similar to Swift’s built-in Decimal type, but allocates bits differently, resulting in a different representable range. (NS)Decimal stores a significand of up to 38 digits long and an exponent from -128 to 127, while this type stores up to 34 digits of significand and an exponent from -6143 to 6144.

    See more

    Declaration

    Swift

    @objc(RealmSwiftDecimal128)
    public final class Decimal128 : RLMDecimal128, Decodable
    extension Decimal128: BSON
    extension Decimal128: Encodable
    extension Decimal128: ExpressibleByIntegerLiteral
    extension Decimal128: ExpressibleByFloatLiteral
    extension Decimal128: ExpressibleByStringLiteral
    extension Decimal128: Comparable
    extension Decimal128: RealmCollectionValue
    extension Decimal128: MinMaxType
    extension Decimal128: AddableType
  • A 12-byte (probably) unique object identifier.

    ObjectIds are similar to a GUID or a UUID, and can be used to uniquely identify objects without a centralized ID generator. An ObjectID consists of:

    1. A 4 byte timestamp measuring the creation time of the ObjectId in seconds since the Unix epoch.
    2. A 5 byte random value
    3. A 3 byte counter, initialized to a random value.

    ObjectIds are intended to be fast to generate. Sorting by an ObjectId field will typically result in the objects being sorted in creation order.

    See more

    Declaration

    Swift

    @objc(RealmSwiftObjectId)
    public final class ObjectId : RLMObjectId, Decodable
    extension ObjectId: BSON
    extension ObjectId: Encodable
    extension ObjectId: Comparable
    extension ObjectId: RealmCollectionValue
  • Object is a class used to define Realm model objects.

    In Realm you define your model classes by subclassing Object and adding properties to be managed. You then instantiate and use your custom subclasses instead of using the Object class directly.

    class Dog: Object {
        @objc dynamic var name: String = ""
        @objc dynamic var adopted: Bool = false
        let siblings = List<Dog>()
    }
    

    Supported property types

    • String, NSString
    • Int
    • Int8, Int16, Int32, Int64
    • Float
    • Double
    • Bool
    • Date, NSDate
    • Data, NSData
    • Decimal128
    • ObjectId
    • @objc enum which has been delcared as conforming to RealmEnum.
    • RealmOptional<Value> for optional numeric properties
    • Object subclasses, to model many-to-one relationships
    • EmbeddedObject subclasses, to model owning one-to-one relationships
    • List<Element>, to model many-to-many relationships

    String, NSString, Date, NSDate, Data, NSData, Decimal128, and ObjectId properties can be declared as optional. Object and EmbeddedObject subclasses must be declared as optional. Int, Int8, Int16, Int32, Int64, Float, Double, Bool, enum, and List properties cannot. To store an optional number, use RealmOptional<Int>, RealmOptional<Float>, RealmOptional<Double>, or RealmOptional<Bool> instead, which wraps an optional numeric value. Lists cannot be optional at all.

    All property types except for List and RealmOptional must be declared as @objc dynamic var. List and RealmOptional properties must be declared as non-dynamic let properties. Swift lazy properties are not allowed.

    Note that none of the restrictions listed above apply to properties that are configured to be ignored by Realm.

    Querying

    You can retrieve all objects of a given type from a Realm by calling the objects(_:) instance method.

    Relationships

    See our Cocoa guide for more details.

    See more

    Declaration

    Swift

    @objc(RealmSwiftObject)
    open class Object : RLMObjectBase, RealmCollectionValue
    extension Object: Combine.ObservableObject
    extension Object: RealmSubscribable
    extension Object: ThreadConfined
  • List is the container type in Realm used to define to-many relationships.

    Like Swift’s Array, List is a generic type that is parameterized on the type it stores. This can be either an Object subclass or one of the following types: Bool, Int, Int8, Int16, Int32, Int64, Float, Double, String, Data, Date, Decimal128, and ObjectId (and their optional versions)

    Unlike Swift’s native collections, Lists are reference types, and are only immutable if the Realm that manages them is opened as read-only.

    Lists can be filtered and sorted with the same predicates as Results<Element>.

    Properties of List type defined on Object subclasses must be declared as let and cannot be dynamic.

    See more

    Declaration

    Swift

    public final class List<Element> : ListBase where Element : RealmCollectionValue
    extension List: ObservableObject, RealmSubscribable
    extension List: RealmCollection
    extension List: MutableCollection
    extension List: Decodable where Element: Decodable
    extension List: Encodable where Element: Encodable
  • EmbeddedObject is a base class used to define embedded Realm model objects.

    Embedded objects work similarly to normal objects, but are owned by a single parent Object (which itself may be embedded). Unlike normal top-level objects, embedded objects cannot be directly created in or added to a Realm. Instead, they can only be created as part of a parent object, or by assigning an unmanaged object to a parent object’s property. Embedded objects are automatically deleted when the parent object is deleted or when the parent is modified to no longer point at the embedded object, either by reassigning an Object property or by removing the embedded object from the List containing it.

    Embedded objects can only ever have a single parent object which links to them, and attempting to link to an existing managed embedded object will throw an exception.

    The property types supported on EmbeddedObject are the same as for Object, except for that embedded objects cannot link to top-level objects, so Object and List<Object> properties are not supported (EmbeddedObject and List<EmbeddedObject> are).

    Embedded objects cannot have primary keys or indexed properties.

    class Owner: Object {
        @objc dynamic var name: String = ""
        let dogs = List<Dog>()
    }
    class Dog: EmbeddedObject {
        @objc dynamic var name: String = ""
        @objc dynamic var adopted: Bool = false
        let owner = LinkingObjects(fromType: Owner.self, property: "dogs")
    }
    
    See more

    Declaration

    Swift

    @objc(RealmSwiftEmbeddedObject)
    open class EmbeddedObject : RLMObjectBase, RealmCollectionValue
    extension EmbeddedObject: ThreadConfined
  • A RealmOptional instance represents an optional value for types that can’t be directly declared as @objc in Swift, such as Int, Float, Double, and Bool.

    To change the underlying value stored by a RealmOptional instance, mutate the instance’s value property.

    See more

    Declaration

    Swift

    public final class RealmOptional<Value> : RLMOptionalBase where Value : RealmOptionalType
    extension RealmOptional: Equatable where Value: Equatable
    extension RealmOptional: Codable where Value: Codable