RLMArray Class Reference
Inherits from | NSObject |
Conforms to | NSFastEnumeration RLMCollection |
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.
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:
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:(RLMObject *)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
firstObject
Returns the first object in the array.
- (id)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:(RLMObject *)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:(RLMObject *)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.
- (id)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
objectAtIndex:
Returns the object at the index specified.
- (id)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 *)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 *)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:(RLMObject *)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 *)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 *)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