Object

open class Object: RLMObjectBase, ThreadConfined

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 {
    dynamic var name: String = ""
    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
  • RealmOptional<T> for optional numeric properties
  • Object subclasses, to model many-to-one relationships
  • List<T>, to model many-to-many relationships

String, NSString, Date, NSDate, Data, NSData and Object subclass properties can be declared as optional. Int, Int8, Int16, Int32, Int64, Float, Double, Bool, 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.

All property types except for List and RealmOptional must be declared as 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.

  • Returns or sets the value of the property with the given name.

    Declaration

    Swift

    open subscript(key: String) -> Any?
  • Creates an unmanaged instance of a Realm object.

    Call add(_:) on a Realm instance to add an unmanaged object into that Realm.

    See

    Realm().add(_:)

    Declaration

    Swift

    public override required init()
  • Creates an unmanaged instance of a Realm object.

    The value argument is used to populate the object. It can be a key-value coding compliant object, an array or dictionary returned from the methods in NSJSONSerialization, or an Array containing one element for each managed property. An exception will be thrown if any required properties are not present and those properties were not defined with default values.

    When passing in an Array as the value argument, all properties must be present, valid and in the same order as the properties defined in the model.

    Call add(_:) on a Realm instance to add an unmanaged object into that Realm.

    Declaration

    Swift

    public init(value: Any)

    Parameters

    value

    The value used to populate the object.

  • The Realm which manages the object, or nil if the object is unmanaged.

    Declaration

    Swift

    public var realm: Realm?
  • The object schema which lists the managed properties for the object.

    Declaration

    Swift

    public var objectSchema: ObjectSchema
  • Indicates if the object can no longer be accessed because it is now invalid.

    An object can no longer be accessed if the object has been deleted from the Realm that manages it, or if invalidate() is called on that Realm.

    Declaration

    Swift

    open override var isInvalidated: Bool
  • A human-readable description of the object.

    Declaration

    Swift

    open override var description: String
  • Helper to return the class name for an Object subclass.

    Declaration

    Swift

    public final override var className: String
  • Undocumented

    Declaration

    Swift

    open class Object: RLMObjectBase, ThreadConfined
  • Override this method to specify the name of a property to be used as the primary key.

    Only properties of types String and Int can be designated as the primary key. Primary key properties enforce uniqueness for each value whenever the property is set, which incurs minor overhead. Indexes are created automatically for primary key properties.

    Declaration

    Swift

    open class func primaryKey() -> String?

    Return Value

    The name of the property designated as the primary key, or nil if the model has no primary key.

  • Override this method to specify the names of properties to ignore. These properties will not be managed by the Realm that manages the object.

    Declaration

    Swift

    open class func ignoredProperties() -> [String]

    Return Value

    An array of property names to ignore.

  • Returns an array of property names for properties which should be indexed.

    Only string, integer, boolean, Date, and NSDate properties are supported.

    Declaration

    Swift

    open class func indexedProperties() -> [String]

    Return Value

    An array of property names.

  • Returns whether two Realm objects are equal.

    Objects are considered equal if and only if they are both managed by the same Realm and point to the same underlying object in the database.

    Declaration

    Swift

    open override func isEqual(_ object: Any?) -> Bool

    Parameters

    object

    The object to compare the receiver to.