Object

public class Object: RLMObjectBase

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 persisted. 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
  • NSDate
  • NSData
  • RealmOptional<T> for optional numeric properties
  • Object subclasses, to model many-to-one relationships
  • List<T: Object>, to model many-to-many relationships

String, NSString, NSDate, 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.

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

    public subscript(key: String) -> AnyObject?
  • Initializes 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()
  • Initializes an unmanaged instance of a Realm object.

    Pass in an Array<AnyObject> or Dictionary<String, AnyObject> instance to set the values of the object’s properties, or any other valid object as described below.

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

    Declaration

    Swift

    public init(value: AnyObject)

    Parameters

    value

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

  • 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 persisted 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

    public override var invalidated: Bool { return super.invalidated }
  • Returns a human-readable description of the object.

    Declaration

    Swift

    public override var description: String { return super.description }
  • A helper property that returns the class name for an Object subclass.

    Declaration

    Swift

    public final override var className: String { return "" }
  • Undocumented

    Declaration

    Swift

    public class Object: RLMObjectBase
  • 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

    public class func primaryKey() -> String? { return nil }

    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 persisted within the Realm that manages the object.

    Declaration

    Swift

    public class func ignoredProperties() -> [String] { return [] }

    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, and NSDate properties are supported.

    Declaration

    Swift

    public class func indexedProperties() -> [String] { return [] }

    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

    public override func isEqual(object: AnyObject?) -> Bool

    Parameters

    object

    The object to compare the receiver to.