RLMArray
@interface RLMArray < RLMObjectType : RLMObject * > : NSObject<RLMCollection,NSFastEnumeration>
RLMArray is the container type in Realm used to define to-many relationships.
Unlike an NSArray, RLMArrays hold a single type, specified by the objectClassName
property.
This is referred to in these docs as the “type” of the array.
When declaring an RLMArray property, the type must be marked as conforming to a
protocol by the same name as the objects it should contain (see the
RLM_ARRAY_TYPE
macro). RLMArray properties can also use Objective‑C generics
if available. For example:
RLM_ARRAY_TYPE(ObjectType)
...
@property RLMArray<ObjectType *><ObjectType> *arrayOfObjectTypes;
RLMArrays can be queried with the same predicates as RLMObject and RLMResults.
RLMArrays cannot be created directly. RLMArray properties on RLMObjects are lazily created when accessed, or can be obtained by querying a Realm.
Key-Value Observing
RLMArray supports array key-value observing on RLMArray properties on RLMObject
subclasses, and the invalidated
property on RLMArray instances themselves is
key-value observing compliant when the RLMArray is attached to a persisted
RLMObject (RLMArrays on standalone RLMObjects will never become invalidated).
Because RLMArrays are attached to the object which they are a property of, they
do not require using the mutable collection proxy objects from
-mutableArrayValueForKey:
or KVC-compatible mutation methods on the containing
object. Instead, you can call the mutation methods on the RLMArray directly.
-
Number of objects in the array.
Declaration
Objective‑C
@property (readonly, assign, nonatomic) NSUInteger count;
-
The class name (i.e. type) of the RLMObjects contained in this RLMArray.
Declaration
Objective‑C
@property (readonly, copy, nonatomic) NSString *_Nonnull objectClassName;
-
The Realm in which this array is persisted. Returns nil for standalone arrays.
Declaration
Objective‑C
@property (readonly, nonatomic, nullable) RLMRealm *realm;
-
Indicates if an array can no longer be accessed.
Declaration
Objective‑C
@property (readonly, getter=isInvalidated, nonatomic) BOOL invalidated;
-
Returns the object at the index specified.
Declaration
Objective‑C
- (nonnull RLMObjectType)objectAtIndex:(NSUInteger)index;
Parameters
index
The index to look up.
-
Returns the first object in the array.
Returns
nil
if called on an empty RLMArray.Declaration
Objective‑C
- (nullable RLMObjectType)firstObject;
-
Returns the last object in the array.
Returns
nil
if called on an empty RLMArray.Declaration
Objective‑C
- (nullable RLMObjectType)lastObject;
-
Adds an object to the end of the array.
Warning
This method can only be called during a write transaction.
Declaration
Objective‑C
- (void)addObject:(nonnull RLMObjectType)object;
Parameters
object
An RLMObject of the class contained by this RLMArray.
-
Adds an array of objects at the end of the array.
Warning
This method can only be called during a write transaction.
Declaration
Objective‑C
- (void)addObjects:(nonnull id<NSFastEnumeration>)objects;
Parameters
objects
An enumerable object such as NSArray or RLMResults which contains objects of the same class as this RLMArray.
-
Inserts an object at the given index.
Throws an exception when called with an index greater than the number of objects in this RLMArray.
Warning
This method can only be called during a write transaction.
Declaration
Objective‑C
- (void)insertObject:(nonnull RLMObjectType)anObject atIndex:(NSUInteger)index;
Parameters
anObject
An object (of the same type as returned from the objectClassName selector).
index
The array index at which the object is inserted.
-
Removes an object at a given index.
Throws an exception when called with an index greater than the number of objects in this RLMArray.
Warning
This method can only be called during a write transaction.
Declaration
Objective‑C
- (void)removeObjectAtIndex:(NSUInteger)index;
Parameters
index
The array index identifying the object to be removed.
-
Removes the last object in an RLMArray.
Warning
This method can only be called during a write transaction.Declaration
Objective‑C
- (void)removeLastObject;
-
Removes all objects from an RLMArray.
Warning
This method can only be called during a write transaction.Declaration
Objective‑C
- (void)removeAllObjects;
-
Replaces an object at the given index with a new object.
Throws an exception when called with an index greater than the number of objects in this RLMArray.
Warning
This method can only be called during a write transaction.
Declaration
Objective‑C
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(nonnull RLMObjectType)anObject;
Parameters
index
The array index of the object to be replaced.
anObject
An object (of the same type as returned from the objectClassName selector).
-
Moves the object at the given source index to the given destination index.
Throws an exception when called with an index greater than or equal to the number of objects in this RLMArray.
Warning
This method can only be called during a write transaction.
Declaration
Objective‑C
- (void)moveObjectAtIndex:(NSUInteger)sourceIndex toIndex:(NSUInteger)destinationIndex;
Parameters
sourceIndex
The index of the object to be moved.
destinationIndex
The index to which the object at
sourceIndex
should be moved. -
Exchanges the objects in the array at given indexes.
Throws an exception when either index exceeds the bounds of this RLMArray.
Warning
This method can only be called during a write transaction.
Declaration
Objective‑C
- (void)exchangeObjectAtIndex:(NSUInteger)index1 withObjectAtIndex:(NSUInteger)index2;
Parameters
index1
The index of the object with which to replace the object at index
index2
.index2
The index of the object with which to replace the object at index
index1
.
-
Gets the index of an object.
Returns NSNotFound if the object is not found in this RLMArray.
Declaration
Objective‑C
- (NSUInteger)indexOfObject:(nonnull RLMObjectType)object;
Parameters
object
An object (of the same type as returned from the objectClassName selector).
-
Gets the index of the first object matching the predicate.
Declaration
Objective‑C
- (NSUInteger)indexOfObjectWhere:(nonnull NSString *)predicateFormat, ...;
Parameters
predicateFormat
The predicate format string which can accept variable arguments.
Return Value
Index of object or NSNotFound if the object is not found in this RLMArray.
-
Gets the index of the first object matching the predicate.
Declaration
Objective‑C
- (NSUInteger)indexOfObjectWithPredicate:(nonnull NSPredicate *)predicate;
Parameters
predicate
The predicate to filter the objects.
Return Value
Index of object or NSNotFound if the object is not found in this RLMArray.
-
Get objects matching the given predicate in the RLMArray.
Declaration
Objective‑C
- (nonnull RLMResults<RLMObjectType> *)objectsWhere: (nonnull NSString *)predicateFormat, ...;
Parameters
predicateFormat
The predicate format string which can accept variable arguments.
Return Value
An RLMResults of objects that match the given predicate
-
Get objects matching the given predicate in the RLMArray.
Declaration
Objective‑C
- (nonnull RLMResults<RLMObjectType> *)objectsWithPredicate: (nonnull NSPredicate *)predicate;
Parameters
predicate
The predicate to filter the objects.
Return Value
An RLMResults of objects that match the given predicate
-
Get a sorted RLMResults from an RLMArray
Declaration
Objective‑C
- (nonnull RLMResults<RLMObjectType> *) sortedResultsUsingProperty:(nonnull NSString *)property ascending:(BOOL)ascending;
Parameters
property
The property name to sort by.
ascending
The direction to sort by.
Return Value
An RLMResults sorted by the specified property.
-
Get a sorted RLMResults from an RLMArray
Declaration
Objective‑C
- (nonnull RLMResults<RLMObjectType> *)sortedResultsUsingDescriptors: (nonnull NSArray *)properties;
Parameters
properties
An array of
RLMSortDescriptor
s to sort by.Return Value
An RLMResults sorted by the specified properties.