RLMArray Class Reference
| Inherits from | NSObject |
| Conforms to | NSFastEnumeration |
| Declared in | RLMArray.h RLMArray.mm |
Overview
RLMArray is the primary container type in Realm.
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 RLMRealm, so you can easily chain queries to further filter query results.
RLMArrays fulfill 2 primary purposes:
- Hold the results of a query. Using one of the query methods on RLMRealm or RLMObject will return a typed RLMArray of results.
- Allow the declaration of one-to-many relationships. See RLMObject class documentation for details.
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
-
countproperty -
objectClassNameproperty -
readOnlyproperty -
realmproperty
Accessing Objects from an Array
Adding, Removing, and Replacing Objects in an Array
-
– addObject: -
– addObjectsFromArray: -
– insertObject:atIndex: -
– removeObjectAtIndex: -
– removeLastObject -
– removeAllObjects -
– replaceObjectAtIndex:withObject:
Querying an Array
-
– indexOfObject: -
– indexOfObjectWhere: -
– indexOfObjectWithPredicate: -
– objectsWhere: -
– objectsWithPredicate: -
– arraySortedByProperty:ascending:
Aggregating Property Values
Serializing an Array to JSON
Unavailable Methods
Properties
count
Number of objects in the array.
@property (nonatomic, readonly, assign) NSUInteger countDeclared In
RLMArray.hobjectClassName
The class name (i.e. type) of the RLMObjects contained in this RLMArray.
@property (nonatomic, readonly, copy) NSString *objectClassNameDeclared In
RLMArray.hClass 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)newDeclared In
RLMArray.hInstance Methods
JSONString
Returns the RLMArray and the RLMObjects it contains as a JSON string.
- (NSString *)JSONStringReturn Value
JSON string representation of this RLMArray.
Declared In
RLMArray.haddObject:
Adds an object to the end of the array.
- (void)addObject:(RLMObject *)objectParameters
- 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.haddObjectsFromArray:
Adds an array of objects at the end of the array.
- (void)addObjectsFromArray:(id)objectsParameters
- objects
An NSArray or RLMArray of objects of the class contained by this RLMArray.
Discussion
Warning: This method can only be called during a write transaction.
Declared In
RLMArray.harraySortedByProperty:ascending:
Get a sorted RLMArray from an existing RLMArray
- (RLMArray *)arraySortedByProperty:(NSString *)property ascending:(BOOL)ascendingParameters
- property
The property name to sort by.
- ascending
The direction to sort by.
Return Value
An RLMArray sorted by the specified property.
Declared In
RLMArray.haverageOfProperty:
Returns the average of a given property for objects in an RLMArray.
- (NSNumber *)averageOfProperty:(NSString *)propertyParameters
- property
The property to calculate average on. Only properties of type int, float and double are supported.
Return Value
The average for the given property amongst objects in an RLMArray. This will be of type double for both float and double properties.
Discussion
NSNumber *average = [table averageOfProperty:@“age”];
Warning: You cannot use this method on RLMObject, RLMArray, and NSData properties.
Declared In
RLMArray.hfirstObject
Returns the first object in the array.
- (id)firstObjectReturn Value
An RLMObject of the class contained by this RLMArray.
Discussion
Returns nil if called on an empty RLMArray.
Declared In
RLMArray.hindexOfObject:
Gets the index of an object.
- (NSUInteger)indexOfObject:(RLMObject *)objectParameters
- 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.hindexOfObjectWhere:
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.hindexOfObjectWithPredicate:
Gets the index of the first object matching the predicate.
- (NSUInteger)indexOfObjectWithPredicate:(NSPredicate *)predicateParameters
- 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.hinit
[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)initDeclared In
RLMArray.hinsertObject:atIndex:
Inserts an object at the given index.
- (void)insertObject:(RLMObject *)anObject atIndex:(NSUInteger)indexParameters
- 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.hlastObject
Returns the last object in the array.
- (id)lastObjectReturn Value
An RLMObject of the class contained by this RLMArray.
Discussion
Returns nil if called on an empty RLMArray.
Declared In
RLMArray.hmaxOfProperty:
Returns the maximum (highest) value of the given property of objects in an RLMArray
- (id)maxOfProperty:(NSString *)propertyParameters
- property
The property to look for a maximum on. Only properties of type int, float and double are supported.
Return Value
The maximum value for the property amongst objects in an RLMArray
Discussion
NSNumber *max = [array maxOfProperty:@“age”];
Warning: You cannot use this method on RLMObject, RLMArray, and NSData properties.
Declared In
RLMArray.hminOfProperty:
Returns the minimum (lowest) value of the given property
- (id)minOfProperty:(NSString *)propertyParameters
- property
The property to look for a minimum on. Only properties of type int, float and double are supported.
Return Value
The minimum value for the property amongst objects in an RLMArray.
Discussion
NSNumber *min = [array minOfProperty:@“age”];
Warning: You cannot use this method on RLMObject, RLMArray, and NSData properties.
Declared In
RLMArray.hobjectAtIndex:
Returns the object at the index specified.
- (id)objectAtIndex:(NSUInteger)indexParameters
- index
The index to look up.
Return Value
An RLMObject of the class contained by this RLMArray.
Declared In
RLMArray.hobjectsWhere:
Get objects matching the given predicate in the RLMArray.
- (RLMArray *)objectsWhere:(NSString *)predicateFormat, ...Parameters
- predicateFormat
The predicate format string which can accept variable arguments.
Return Value
An RLMArray of objects that match the given predicate
Declared In
RLMArray.hobjectsWithPredicate:
Get objects matching the given predicate in the RLMArray.
- (RLMArray *)objectsWithPredicate:(NSPredicate *)predicateParameters
- predicate
The predicate to filter the objects.
Return Value
An RLMArray of objects that match the given predicate
Declared In
RLMArray.hremoveAllObjects
Removes all objects from an RLMArray.
- (void)removeAllObjectsDiscussion
Warning: This method can only be called during a write transaction.
Declared In
RLMArray.hremoveLastObject
Removes the last object in an RLMArray.
- (void)removeLastObjectDiscussion
Warning: This method can only be called during a write transaction.
Declared In
RLMArray.hremoveObjectAtIndex:
Removes an object at a given index.
- (void)removeObjectAtIndex:(NSUInteger)indexParameters
- 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.hreplaceObjectAtIndex:withObject:
Replaces an object at the given index with a new object.
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(RLMObject *)anObjectParameters
- 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.hsumOfProperty:
Returns the sum of the given property for objects in an RLMArray.
- (NSNumber *)sumOfProperty:(NSString *)propertyParameters
- property
The property to calculate sum on. Only properties of type int, float and double are supported.
Return Value
The sum of the given property over all objects in an RLMArray.
Discussion
NSNumber *sum = [array sumOfProperty:@“age”];
Warning: You cannot use this method on RLMObject, RLMArray, and NSData properties.
Declared In
RLMArray.h