RLMArray Class Reference
Declared in | RLMArray.h RLMArray.mm |
Overview
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.
Tasks
RLMArray Properties
-
count
property -
objectClassName
property -
realm
property -
invalidated
property
Accessing Objects from an Array
Adding, Removing, and Replacing Objects in an Array
-
– addObject:
-
– addObjects:
-
– insertObject:atIndex:
-
– removeObjectAtIndex:
-
– removeLastObject
-
– removeAllObjects
-
– replaceObjectAtIndex:withObject:
-
– moveObjectAtIndex:toIndex:
-
– exchangeObjectAtIndex:withObjectAtIndex:
Querying an Array
-
– indexOfObject:
-
– indexOfObjectWhere:
-
– indexOfObjectWithPredicate:
-
– objectsWhere:
-
– objectsWithPredicate:
-
– sortedResultsUsingProperty:ascending:
-
– sortedResultsUsingDescriptors:
Unavailable Methods
Properties
count
Number of objects in the array.
@property (nonatomic, readonly, assign) NSUInteger count
Declared In
RLMArray.h
invalidated
Indicates if an array can no longer be accessed.
@property (nonatomic, readonly, getter=isInvalidated) BOOL invalidated
Declared In
RLMArray.h
Class Methods
new
[RLMArray new] is not available because RLMArrays cannot be created directly. RLMArray properties on RLMObjects are lazily created when accessed, or can be obtained by querying a Realm.
+ (instancetype)new
Declared In
RLMArray.h
Instance Methods
addObject:
Adds an object to the end of the array.
- (void)addObject:(RLMObjectArgument)object
Parameters
- object
An RLMObject of the class contained by this RLMArray.
Discussion
Warning: This method can only be called during a write transaction.
Declared In
RLMArray.h
addObjects:
Adds an array of objects at the end of the array.
- (void)addObjects:(id<NSFastEnumeration>)objects
Parameters
- objects
An enumerable object such as NSArray or RLMResults which contains objects of the same class as this RLMArray.
Discussion
Warning: This method can only be called during a write transaction.
Declared In
RLMArray.h
exchangeObjectAtIndex:withObjectAtIndex:
Exchanges the objects in the array at given indexes.
- (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
.
Discussion
Throws an exception when either index exceeds the bounds of this RLMArray.
Warning: This method can only be called during a write transaction.
Declared In
RLMArray.h
firstObject
Returns the first object in the array.
- (nullable RLMObjectType)firstObject
Return Value
An RLMObject of the class contained by this RLMArray.
Discussion
Returns nil
if called on an empty RLMArray.
Declared In
RLMArray.h
indexOfObject:
Gets the index of an object.
- (NSUInteger)indexOfObject:(RLMObjectArgument)object
Parameters
- object
An object (of the same type as returned from the objectClassName selector).
Discussion
Returns NSNotFound if the object is not found in this RLMArray.
Declared In
RLMArray.h
indexOfObjectWhere:
Gets the index of the first object matching the predicate.
- (NSUInteger)indexOfObjectWhere:(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.
Declared In
RLMArray.h
indexOfObjectWithPredicate:
Gets the index of the first object matching the predicate.
- (NSUInteger)indexOfObjectWithPredicate:(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.
Declared In
RLMArray.h
init
[RLMArray init] is not available because RLMArrays cannot be created directly. RLMArray properties on RLMObjects are lazily created when accessed, or can be obtained by querying a Realm.
- (instancetype)init
Declared In
RLMArray.h
insertObject:atIndex:
Inserts an object at the given index.
- (void)insertObject:(RLMObjectArgument)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.
Discussion
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.
Declared In
RLMArray.h
lastObject
Returns the last object in the array.
- (nullable RLMObjectType)lastObject
Return Value
An RLMObject of the class contained by this RLMArray.
Discussion
Returns nil
if called on an empty RLMArray.
Declared In
RLMArray.h
moveObjectAtIndex:toIndex:
Moves the object at the given source index to the given destination index.
- (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.
Discussion
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.
Declared In
RLMArray.h
objectAtIndex:
Returns the object at the index specified.
- (RLMObjectType)objectAtIndex:(NSUInteger)index
Parameters
- index
The index to look up.
Return Value
An RLMObject of the class contained by this RLMArray.
Declared In
RLMArray.h
objectsWhere:
Get objects matching the given predicate in the RLMArray.
- (RLMResults RLM_GENERIC_RETURN *)objectsWhere:(NSString *)predicateFormat, ...
Parameters
- predicateFormat
The predicate format string which can accept variable arguments.
Return Value
An RLMResults of objects that match the given predicate
Declared In
RLMArray.h
objectsWithPredicate:
Get objects matching the given predicate in the RLMArray.
- (RLMResults RLM_GENERIC_RETURN *)objectsWithPredicate:(NSPredicate *)predicate
Parameters
- predicate
The predicate to filter the objects.
Return Value
An RLMResults of objects that match the given predicate
Declared In
RLMArray.h
removeAllObjects
Removes all objects from an RLMArray.
- (void)removeAllObjects
Discussion
Warning: This method can only be called during a write transaction.
Declared In
RLMArray.h
removeLastObject
Removes the last object in an RLMArray.
- (void)removeLastObject
Discussion
Warning: This method can only be called during a write transaction.
Declared In
RLMArray.h
removeObjectAtIndex:
Removes an object at a given index.
- (void)removeObjectAtIndex:(NSUInteger)index
Parameters
- index
The array index identifying the object to be removed.
Discussion
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.
Declared In
RLMArray.h
replaceObjectAtIndex:withObject:
Replaces an object at the given index with a new object.
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(RLMObjectArgument)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).
Discussion
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.
Declared In
RLMArray.h
sortedResultsUsingDescriptors:
Get a sorted RLMResults from an RLMArray
- (RLMResults RLM_GENERIC_RETURN *)sortedResultsUsingDescriptors:(NSArray *)properties
Parameters
- properties
An array of
RLMSortDescriptor
s to sort by.
Return Value
An RLMResults sorted by the specified properties.
Declared In
RLMArray.h
sortedResultsUsingProperty:ascending:
Get a sorted RLMResults from an RLMArray
- (RLMResults RLM_GENERIC_RETURN *)sortedResultsUsingProperty:(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.
Declared In
RLMArray.h