Object

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
  • Float
  • Double
  • Bool
  • NSDate
  • NSData
  • RealmOptional<T> for optional numeric properties
  • Object subclasses for to-one relationships
  • List<T: Object> for to-many relationships

String, NSString, NSDate, NSData and Object subclass properties can be optional. Int, Float, Double, Bool and List properties cannot. To store an optional number, instead use RealmOptional<Int>, RealmOptional<Float>, RealmOptional<Double>, or RealmOptional<Bool> instead, which wraps an optional value of the generic type.

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 gets Results of an Object subclass via tha objects(_:) free function or the objects(_:) instance method on Realm.

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?
  • Initialize a standalone (unpersisted) Object. Call add(_:) on a Realm to add standalone objects to a realm.

    Declaration

    Swift

    public required override init()
  • Initialize a standalone (unpersisted) Object with values from an Array<AnyObject> or Dictionary<String, AnyObject>. Call add(_:) on a Realm to add standalone objects to a 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 a JSON object such as those returned from the methods in NSJSONSerialization, or an Array with one object for each persisted property. An exception will be thrown if any required properties are not present and no default is set.

  • The Realm this object belongs to, or nil if the object does not belong to a realm (the object is standalone).

    Declaration

    Swift

    public var realm: Realm?
  • The ObjectSchema which lists the persisted properties for this object.

    Declaration

    Swift

    public var objectSchema: ObjectSchema
  • Indicates if an object can no longer be accessed.

    An object can no longer be accessed if the object has been deleted from the containing realm or if invalidate is called on the containing realm.

    Declaration

    Swift

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

    Declaration

    Swift

    public override var description: String { return super.description }
  • Helper to return the class name for an Object subclass.

    Declaration

    Swift

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

    Declaration

    Swift

    public class Object: RLMObjectBase
  • Override to designate a property as the primary key for an Object subclass. Only properties of type 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 some overhead. Indexes are created automatically for primary key properties.

    Declaration

    Swift

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

    Return Value

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

  • Override to return an array of property names to ignore. These properties will not be persisted and are treated as transient.

    Declaration

    Swift

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

    Return Value

    Array of property names to ignore.

  • Return an array of property names for properties which should be indexed. Only supported for string and int properties.

    Declaration

    Swift

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

    Return Value

    Array of property names to index.

  • Get an Array of objects of type className which have this object as the given property value. This can be used to get the inverse relationship value for Object and List properties.

    Declaration

    Swift

    public func linkingObjects<T: Object>(type: T.Type, forProperty propertyName: String) -> [T]

    Parameters

    className

    The type of object on which the relationship to query is defined.

    property

    The name of the property which defines the relationship.

    Return Value

    An Array of objects of type className which have this object as their value for the propertyName property.

  • Returns whether both objects are equal. Objects are considered equal when they are both from the same Realm and point to the same underlying object in the database.

    Declaration

    Swift

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